-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReconstructQueue.java
More file actions
49 lines (43 loc) · 1.21 KB
/
ReconstructQueue.java
File metadata and controls
49 lines (43 loc) · 1.21 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
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
/**
* @description:
* @Author: JachinDo
* @Date: 2019/10/25 19:17
*/
public class ReconstructQueue {
public int[][] reconstructQueue(int[][] people) {
int len = people.length;
Arrays.sort(people, new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
if (o1[0] > o2[0]) {
return -1;
} else if (o1[0] < o2[0]) {
return 1;
} else {
if (o1[1] > o2[1]) {
return 1;
} else {
return -1;
}
}
}
});
int end = 0;
int head = 0;
ArrayList<int[]> list = new ArrayList<>();
while (end < len) {
while (end < len-1 && people[end + 1][0] == people[head][0]) {
end++;
}
for (int i = head; i <= end; i++) {
list.add(people[i][1], people[i]);
}
head = end + 1;
end = head;
}
return list.toArray(new int[len][2]);
}
}