-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path75. Sort Colors
34 lines (31 loc) · 966 Bytes
/
75. Sort Colors
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
// time complexity : O(N)
// Space complexity : O(1)
// Runtime: 0 ms, faster than 100.00% of Java online submissions for Sort Colors.
// Memory Usage: 37.5 MB, less than 61.01% of Java online submissions for Sort Colors.
class Solution {
public void sortColors(int[] nums) {
int low=0;
int mid=0;
int high=nums.length-1;
int temp;
while(mid<=high)
{
switch(nums[mid]){
case 0: temp=nums[low];
nums[low]=nums[mid];
nums[mid]=temp;
mid++;
low++;
break;
case 1:mid++;
break;
case 2:temp=nums[mid];
nums[mid]=nums[high];
nums[high]=temp;
high--;
break;
}
}
//sorted
}
}