-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path143. 排颜色 II.cpp
38 lines (36 loc) · 926 Bytes
/
143. 排颜色 II.cpp
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
class Solution {
public:
/*
* @param colors: A list of integer
* @param k: An integer
* @return: nothing
*/
int parttion(vector<int> &v, int low, int high)
{
int pivotkey = v[low];
while(low<high)
{
while(low<high && pivotkey<=v[high])
high--;
v[low] = v[high];
while(low<high && pivotkey>=v[low])
low++;
v[high] = v[low];
}
v[low] = pivotkey;
return low;
}
void quickSort(vector<int> &v, int low, int high)
{
while(low<high)
{
int pivot = parttion(v, low, high);
quickSort(v, low, pivot);
low = pivot + 1;
}
}
void sortColors2(vector<int> &colors, int k) {
// write your code here
quickSort(colors, 0, colors.size()-1);
}
};