-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmoveZero.cpp
More file actions
56 lines (44 loc) · 1.33 KB
/
moveZero.cpp
File metadata and controls
56 lines (44 loc) · 1.33 KB
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
46
47
48
49
50
51
52
53
54
55
56
/* leetcode 283. Move Zeros
Given an integer array nums, move all 0's to the end of it while maintaining the relative order of the non-zero elements.
Note that you must do this in-place without making a copy of the array.
Input: nums = [0,1,0,3,12]
Output: [1,3,12,0,0]
*/
#include<iostream>
#include<vector>
using namespace std;
int main(){
vector<int> arr = {0,1,0,3,12};
int zeroCount = 0;
// move all the nonzero elements advance
for(int i=0; i < arr.size(); i++){
if(arr[i]!=0){
arr[zeroCount++] = arr[i];
}
}
for(; zeroCount < arr.size(); zeroCount++){
arr[zeroCount] = 0;
}
for(int i=0; i < arr.size(); i++){
cout << arr[i] << " ";
}
return 0;
}
/* in Leetcode next code will runtime error*/
// int main(){
// vector<int> arr = {0,1,0,3,12};
// int nextNonZeroLoc = 0, nowLoc = 0;
// int arrLen = arr.size();
// while (nowLoc < arrLen-1 && nextNonZeroLoc < arrLen-1){
// while (!arr[nextNonZeroLoc]){nextNonZeroLoc++;}
// if (!arr[nowLoc]){
// swap(arr[nowLoc], arr[nextNonZeroLoc]);
// cout << nowLoc << " " << nextNonZeroLoc << endl;
// nowLoc++;
// }
// }
// for(int i=0; i<arrLen; i++){
// cout << arr[i] << " ";
// }
// return 0;
// }