-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntersectionArray.java
More file actions
33 lines (29 loc) · 956 Bytes
/
IntersectionArray.java
File metadata and controls
33 lines (29 loc) · 956 Bytes
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
class Solution {
public int[][] intervalIntersection(int[][] A, int[][] B) {
List<int[]> out = new ArrayList<>();
if(A.length==0 || B.length==0 )
return new int[0][];
int a=0, b=0,c=0;
int astart = A[0][0], aend=A[0][1], bstart=B[0][0], bend = B[0][1], ostart=0, oend=0;
while(a<A.length && b<B.length){
if(A[a][0]>= B[b][0]){
ostart = A[a][0];
}
else{
ostart=B[b][0];
}
if(A[a][1]>= B[b][1]){
oend = B[b][1];
b++;
}
else{
oend=A[a][1];
a++;
}
// System.out.println("hello: "+ostart+","+oend);
if(ostart<=oend)
out.add(new int[]{ostart,oend});
}
return out.toArray(new int[out.size()][]);
}
}