forked from striver79/SDESheet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsort012Java
30 lines (30 loc) · 802 Bytes
/
sort012Java
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
class Solution {
public void sortColors(int[] nums) {
int lo = 0;
int hi = nums.length - 1;
int mid = 0;
int temp;
while (mid <= hi) {
switch (nums[mid]) {
case 0: {
temp = nums[lo];
nums[lo] = nums[mid];
nums[mid] = temp;
lo++;
mid++;
break;
}
case 1:
mid++;
break;
case 2: {
temp = nums[mid];
nums[mid] = nums[hi];
nums[hi] = temp;
hi--;
break;
}
}
}
}
}