|
1 | 1 | classSolution {
|
2 |
| -publicint[][]intervalIntersection(int[][]A,int[][]B) { |
| 2 | +publicint[][]intervalIntersection(int[][]firstList,int[][]secondList) { |
3 | 3 | List<int[]>list =newArrayList<>();
|
4 |
| -intstartA =0; |
5 |
| -intstartB =0; |
6 |
| -while (startA <A.length &&startB <B.length) { |
7 |
| -intintervalStart =Math.max(A[startA][0],B[startB][0]); |
8 |
| -intintervalEnd =Math.min(A[startA][1],B[startB][1]); |
9 |
| -if (intervalStart <=intervalEnd) { |
10 |
| -list.add(newint[]{intervalStart,intervalEnd}); |
| 4 | +intidxOne =0; |
| 5 | +intidxTwo =0; |
| 6 | +while (idxOne <firstList.length &&idxTwo <secondList.length) { |
| 7 | +intmaxStart =Math.max(firstList[idxOne][0],secondList[idxTwo][0]); |
| 8 | +intminEnd =Math.min(firstList[idxOne][1],secondList[idxTwo][1]); |
| 9 | +if (maxStart <=minEnd) { |
| 10 | +list.add(newint[]{maxStart,minEnd}); |
11 | 11 | }
|
12 |
| -if (A[startA][1] <B[startB][1]) { |
13 |
| -startA++; |
14 |
| - } |
15 |
| -else { |
16 |
| -startB++; |
| 12 | +if (minEnd ==firstList[idxOne][1]) { |
| 13 | +idxOne++; |
| 14 | + }else { |
| 15 | +idxTwo++; |
17 | 16 | }
|
18 | 17 | }
|
19 |
| -int[][]ans =newint[list.size()][2]; |
20 |
| -returnlist.toArray(ans); |
| 18 | +int[][]result =newint[list.size()][2]; |
| 19 | +returnlist.toArray(result); |
21 | 20 | }
|
22 | 21 | }
|