-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0283_MoveZeros.cpp
More file actions
39 lines (34 loc) · 904 Bytes
/
Copy path0283_MoveZeros.cpp
File metadata and controls
39 lines (34 loc) · 904 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <fmt/ranges.h>
#include <vector>
using namespace std;
class Solution
{
public:
void moveZeroes(vector<int> &nums)
{
// [A] using ranges
const auto [first, last] = ranges::remove(nums, 0);
ranges::fill(first, nums.end(), 0);
// [B] using two pointers
// auto nz = nums.begin();
// for (auto it = nums.begin(); it != nums.end(); ++it) {
// if (*it != 0) {
// std::swap(*it, *nz);
// ++nz;
// }
// }
// ranges::fill(nz, nums.end(), 0);
}
};
int main()
{
Solution sol;
// TEST CASE
std::vector<int> nums1{0, 1, 0, 3, 12};
std::vector<int> nums2{0};
for (auto num : {nums1, nums2}) {
fmt::print("Before: {}\n", fmt::join(nums1, ", "));
sol.moveZeroes(nums1);
fmt::print("After: {}\n\n", fmt::join(nums1, ", "));
}
}