-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmoveZeroes.cpp
More file actions
40 lines (36 loc) · 762 Bytes
/
moveZeroes.cpp
File metadata and controls
40 lines (36 loc) · 762 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
#include <iostream>
#include <vector>
using namespace std;
void moveZeroes(vector<int>& nums) {
if (nums.size() == 0)
return;
int start = 0, count = 0;
for (int i = 0; i < nums.size(); ++i) {
if (nums[i] != 0)
nums[start++] = nums[i];
else
count++;
}
for (int i = nums.size() - count; i < nums.size(); ++i)
nums[i] = 0;
}
int main()
{
vector<int> nums;
int tmp;
char c;
cout << "Please input the array:";
while (cin >> tmp) {
nums.push_back(tmp);
if ((c = cin.get()) == '\n')
break;
}
for (int i = 0; i < nums.size(); ++i)
cout << nums[i] << " ";
cout << endl;
moveZeroes(nums);
for (int i = 0; i < nums.size(); ++i)
cout << nums[i] << " ";
cout << endl;
return 0;
}