LeetCode 75. Sort Colors [Medium]

题目来源:https://leetcode.com/problems/sort-colors/

题目难度:Medium

解答1[Java]:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
public void sortColors(int[] nums) {
int zero = -1;
int two = nums.length;
int temp;

for (int i = 0; i < two;) {
if (nums[i] == 1)
i++;
else if (nums[i] == 2) {
temp = nums[--two];
nums[two] = nums[i];
nums[i] = temp;
} else {
temp = nums[++zero];
nums[zero] = nums[i];
nums[i++] = temp;
}
}
}
}

三路快排的思路