-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path23-IntervalListIntersection.java
71 lines (61 loc) · 1.75 KB
/
23-IntervalListIntersection.java
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
class Solution {
public int[][] intervalIntersection(int[][] A, int[][] B) {
int lenA = A.length;
int lenB = B.length;
if (lenA == 0 || lenB == 0) return new int[0][0];
List<List<Integer>> arr = new ArrayList<List<Integer>>();
int j = 0, i = 0;
while (j < lenA && i < lenB) {
int lb = B[i][0], hb = B[i][1];
if (A[j][1] < lb) {
j++;
continue;
}
if (hb < A[j][0]) {
i++;
continue;
}
if (A[j][1] <= hb) {
if (A[j][0] < lb) {
arr.add(new ArrayList<>(Arrays.asList(lb, A[j][1])));
j++;
continue;
}
if (A[j][0] >= lb) {
arr.add(new ArrayList<>(Arrays.asList(A[j][0], A[j][1])));
j++;
continue;
}
}
if (A[j][1] > hb) {
if (A[j][0] >= lb) {
arr.add(new ArrayList<>(Arrays.asList(A[j][0], hb)));
i++;
continue;
}
if (A[j][0] < lb) {
arr.add(new ArrayList<>(Arrays.asList(lb, hb)));
i++;
continue;
}
}
}
int len = arr.size();
int[][] ans = new int[len][2];
for (int k = 0; k < len; k++) {
ans[k][0] = arr.get(k).get(0);
ans[k][1] = arr.get(k).get(1);
}
return ans;
}
}
// [0,2],[5,10],[13,23],[24,25]
// [1,5],[8,12],[15,24],[25,26]
// ------
// -----
// -----
// -------
// -----
// -----
// -------
// ----