-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmovezero.cpp
More file actions
42 lines (35 loc) · 716 Bytes
/
movezero.cpp
File metadata and controls
42 lines (35 loc) · 716 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
40
41
42
#include<bits/stdc++.h>
using namespace std;
void moveZeroes(vector<int>& nums) {
int n = nums.size();
int i = 0;
int j = i+1;
while(j < n){
if(nums[i] == 0){
if(nums[j] != 0){
swap(nums[i], nums[j]);
i++;
j++;
}else{
j++;
}
}else{
i++;
j++;
}
}
}
int main(){
vector <int> test = {0,1,0,3,12}; // [1,3,12,0,0]
vector <int> test2 = {0}; // [0]
moveZeroes(test);
moveZeroes(test2);
for(int i : test){
cout << i << " ";
}
cout << endl;
for(int i : test2){
cout << i << " ";
}
return 0;
}