Skip to content

Commit 5730fe5

Browse files
authored
Create 75. Sort colors
1 parent c22efba commit 5730fe5

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

75. Sort colors

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
public class Solution {
2+
public void sortColors(int[] nums) {
3+
4+
// 3 different values, it matters!
5+
// p1 put 0. p2 put 2, and then 1 will be moved in to middle
6+
int p1 = 0, p2 = nums.length - 1, index = 0;
7+
while (index <= p2) {
8+
if (nums[index] == 0) {
9+
nums[index] = nums[p1];
10+
nums[p1] = 0;
11+
p1++;
12+
}
13+
if (nums[index] == 2) {
14+
nums[index] = nums[p2];
15+
nums[p2] = 2;
16+
p2--;
17+
index--;
18+
}
19+
index++;
20+
21+
}
22+
}
23+
}

0 commit comments

Comments
 (0)