We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 8c6a4f4 commit 4af02efCopy full SHA for 4af02ef
Two Pointers/75_Sort_Colors.java
@@ -1,23 +1,23 @@
1
class Solution {
2
public void sortColors(int[] nums) {
3
- if (nums == null || nums.length <= 1) {
+ if (nums == null || nums.length == 0) {
4
return;
5
}
6
7
- int j = 0, k = nums.length - 1;
+ int i = 0, j = 0, k = nums.length - 1;
8
9
- for (int i = 0; i <= k; i++) {
10
- if (nums[i] == 0) {
+ while (j <= k) {
+ if (nums[j] == 0) {
11
swap(nums, i, j);
12
- j++;
13
- } else if (nums[i] == 2) {
14
- swap(nums, i, k);
15
- i--;
16
- k--;
+ ++i;
+ ++j;
+ } else if (nums[j] == 2) {
+ swap(nums, j, k);
+ --k;
17
+ } else {
18
19
20
-
- return;
21
22
23
private void swap(int[] nums, int i, int j) {
0 commit comments