Skip to content

Commit 1fc4f28

Browse files
authored
add cpp version, use three way partition algorithm
1 parent 5fb7995 commit 1fc4f28

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// 三路partition算法(0ms)
2+
class Solution {
3+
public:
4+
void sortColors(vector<int>& nums) {
5+
vector<int>::iterator next_lower = nums.begin() ;
6+
vector<int>::iterator next_cmp = nums.begin() ;
7+
vector<int>::iterator next_bigger = nums.end()-1 ;
8+
9+
while (next_cmp <= next_bigger)
10+
{
11+
if (*next_cmp < 1)
12+
swap(*next_lower++, *next_cmp++) ;
13+
else if (*next_cmp > 1)
14+
swap(*next_bigger--, *next_cmp) ;
15+
else
16+
++next_cmp ;
17+
}
18+
}
19+
};

0 commit comments

Comments
 (0)