-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path283_move_zeroes.cpp
44 lines (35 loc) · 911 Bytes
/
283_move_zeroes.cpp
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
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
// O(N)
class Solution {
public:
void moveZeroes(std::vector<int>& nums) {
int frst_zero = -1;
for (int i = 0; i < nums.size(); i++){
if (nums.at(i) == 0 && frst_zero == -1)
frst_zero = i;
else if (nums.at(i) != 0 && frst_zero != -1){
std::swap(nums.at(i), nums.at(frst_zero));
frst_zero++;
}
}
}
};
int main(){
std::cout << "Space separated array: ";
std::string line;
std::getline(std::cin, line);
std::istringstream ss(line);
int v;
std::vector<int> nums;
while (ss >> v)
nums.push_back(v);
Solution().moveZeroes(nums);
std::cout << "Zeros moved: ";
for (auto v : nums)
std::cout << v << ' ';
std::cout << std::endl;
return 0;
}