-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy path75.cpp
More file actions
30 lines (28 loc) · 712 Bytes
/
75.cpp
File metadata and controls
30 lines (28 loc) · 712 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
class Solution {
public:
void sortColors(vector<int>& nums) {
int redcount = 0, whitecount = 0;
for(int i = 0;i < nums.size();i++){
if (nums[i] == 0)
redcount++;
else if(nums[i] == 1)
whitecount++;
else if(nums[i] == 2)
continue;
else
return;
}
for(int i = 0;i<nums.size();i++){
if(redcount){
nums[i] = 0;
redcount--;
}
else if(whitecount){
nums[i] = 1;
whitecount--;
}
else
nums[i] = 2;
}
}
};