-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path283 Move Zeroes.cpp
More file actions
45 lines (37 loc) · 944 Bytes
/
283 Move Zeroes.cpp
File metadata and controls
45 lines (37 loc) · 944 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
43
44
45
static int fastio=[](){
std::ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
return 0;
}();
class Solution {
public:
void moveZeroes(vector<int>& nums) {
int n=nums.size();
if(n<2)
return;
int i=0,j;
// computation will start after getting the first 0
while(i<n){
if(nums[i]==0)
break;
++i;
}
j=i+1;
while(i<n && j<n){
if(nums[i]==0 && nums[j]!=0){
// swap
// int t=nums[i];
// nums[i]=nums[j];
// nums[j]=t;
swap(nums[i],nums[j]);
++i;
++j;
}
if(j<n && nums[j]==0)
++j;
if(i<n && nums[i]!=0)
++i;
}
}
};