Skip to content

Commit 6da4304

Browse files
committed
add: move negative numbers
1 parent 02bcfa1 commit 6da4304

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

Diff for: array/MoveNegative.cpp

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
class Solution {
6+
public:
7+
void moveNegative(vector<int>& prices) {
8+
int j = 0;
9+
for (int i = 0; i < prices.size(); i++) {
10+
if (prices[i] < 0) {
11+
if(i != j) swap(prices[i], prices[j]);
12+
j++;
13+
}
14+
}
15+
}
16+
17+
void printArray(vector<int>& prices) {
18+
for(auto it: prices) {
19+
cout << it << ", ";
20+
}
21+
cout << endl;
22+
}
23+
};
24+
25+
int main() {
26+
Solution sol;
27+
vector<int> test = { -1, 2, -3, 4, 5, 6, -7, 8, 9 };
28+
sol.moveNegative(test);
29+
sol.printArray(test);
30+
return 0;
31+
}

0 commit comments

Comments
 (0)