-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSortColors.java
More file actions
120 lines (110 loc) · 3.43 KB
/
Copy pathSortColors.java
File metadata and controls
120 lines (110 loc) · 3.43 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package Void;
import org.junit.Test;
/**
* @description: 描述 Medium
* @author: dekai.kong
* @date: 2018-12-24 15:00
* @from https://leetcode.com/problems/sort-colors/
* Given an array with n objects colored red, white or blue, sort them in-place so that objects of the same color are adjacent, with the colors in the order red, white and blue.
*
* Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.
*
* Note: You are not suppose to use the library's sort function for this problem.
*
* Example:
*
* Input: [2,0,2,1,1,0]
* Output: [0,0,1,1,2,2]
* Follow up:
*
* A rather straight forward solution is a two-pass algorithm using counting sort.
* First, iterate the array counting number of 0's, 1's, and 2's, then overwrite array with total number of 0's, then 1's and followed by 2's.
* Could you come up with a one-pass algorithm using only constant space?
* TODO 经典双指针
*/
public class SortColors {
public SortColors() {
}
/**
* Runtime: 4 ms, faster than 2.33% of Java online submissions for Sort Colors.
* @param nums
*/
public void sortColors2(int[] nums) {
int eix = nums.length-1;
int temp;
int zix = 0;
for (int i = zix; i <= eix; i++) {
while (nums[i] != 0 && i < eix){
if(nums[i] == 1){
zix = i+1;
while(nums[eix] == 2){
eix--;
}
for (int j = zix; j <= eix; j++) {
if(nums[j] == 0){
temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
break;
}
}
break;
}
temp = nums[i];
nums[i] = nums[eix];
nums[eix] = temp;
eix --;
}
}
System.out.println(nums);
}
/**
* 计数排序 先计数每个数的个数
* Runtime: 0 ms, faster than 100.00% of Java online submissions for Sort Colors.
* @param nums
*/
public void sortColors3(int[] nums) {
int[] len = new int[3];
int inx = 0;
for (int i = 0; i < nums.length; i++) {
len[nums[i]] += 1;
}
for (int i = 0; i < 3; i++){
for (int j = 0; j < len[i]; ++j) {
nums[inx++] = i;
}
}
}
/**
* 双指针遍历,st记录的是最后一个0之后的一位
* Runtime: 0 ms, faster than 100.00% of Java online submissions for Sort Colors.
* @param nums
*/
public void sortColors(int[] nums) {
int st = 0;
int en = nums.length-1;
int temp;
for (int i = 0; i <= en; i++) {
if(nums[i] == 0){
temp = nums[i];
nums[i] = nums[st];
nums[st] = temp;
st++;
}else if(nums[i] == 2){
temp = nums[i];
nums[i] = nums[en];
nums[en] = temp;
en--;
i--;
}
}
}
@Test
public void test() {
sortColors(new int[]{2,0,2,1,1,0});
sortColors(new int[]{2,0,2,1,0,1,0});
sortColors(new int[]{1,2,0});
sortColors(new int[]{1,0,2});
sortColors(new int[]{1,1,0,1,2});
}
}