-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmove_zeroes.cpp
More file actions
38 lines (35 loc) · 760 Bytes
/
move_zeroes.cpp
File metadata and controls
38 lines (35 loc) · 760 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
// 283. Move Zeroes: https://leetcode.com/problems/move-zeroes
// Author: xianfeng.zhu@gmail.com
#include <stdio.h>
#include <algorithm>
#include <vector>
class Solution
{
public:
void moveZeroes(std::vector<int>& nums)
{
int zero_idx = 0;
for (int i = 0; i < nums.size(); i++)
{
if (nums[i] != 0)
{
if (i != zero_idx)
{
std::swap(nums[i], nums[zero_idx]);
}
zero_idx++;
}
}
}
};
int main(int argc, char* argv[])
{
std::vector<int> nums = {0, 1, 0, 3, 12};
Solution().moveZeroes(nums);
for (auto val: nums)
{
printf("%d ", val);
}
printf("\n");
return 0;
}