-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSortColors.cpp
More file actions
43 lines (33 loc) · 895 Bytes
/
SortColors.cpp
File metadata and controls
43 lines (33 loc) · 895 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
41
42
43
/*Given an array nums with n objects colored red, white, or blue, sort them in-place so that objects of the same color are adjacent, with the colors in the order red, white, and blue.
We will use the integers 0, 1, and 2 to represent the color red, white, and blue, respectively.*/
#include<bits/stdc++.h>
using namespace std;
// void SortColors(vector<int>& nums){
// sort(nums.begin(), nums.end());
// }
void SortColors(vector<int>& nums){
int red=0, white=0, blue=0;
for(int i=0; i<nums.size(); i++){
if(nums[i]==0) red++;
else if(nums[i]==1) white++;
else blue++;
}
nums.clear();
for(int i=0; i<red; i++)
nums.push_back(0);
for(int i=0; i<white; i++)
nums.push_back(1);
for(int i=0; i<blue; i++)
nums.push_back(2);
}
int main(){
int n, x;
cin>>n;
vector<int> v;
for(int i=0; i<n; i++){
cin>>x;
v.push_back(x);
}
SortColors(v);
for(auto it:v) cout<<it;
}