Skip to content

Commit e31f2be

Browse files
authored
Create sort_012.cpp
1 parent 0bf6bdd commit e31f2be

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

sort_012.cpp

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public:
3+
void sortColors(vector<int>& nums) {
4+
int right=nums.size()-1,left=0;
5+
int x=0;
6+
while(x<=right){
7+
if(nums[x]==0){
8+
swap(nums[x],nums[left]);
9+
left++,x++;
10+
}
11+
else if(nums[x]==1){
12+
x++;
13+
}
14+
else{
15+
swap(nums[x],nums[right]);
16+
right--;
17+
}
18+
}
19+
20+
}
21+
};

0 commit comments

Comments
 (0)