---
# System prepended metadata

title: 0986. Interval List Intersections
tags: [Leetcode, Merge Interval, FaceBook, Medium]

---

# 0986. Interval List Intersections
###### tags: `Leetcode` `FaceBook` `Medium` `Merge Interval`
Link: https://leetcode.com/problems/interval-list-intersections/

## 思路
和[56.Merge Interval](https://leetcode.com/problems/merge-intervals/)一样
都是每次拿两个interval，比较左端点或右端点，不会再更改答案了之后把结果放在array里面
## Code
```java=
class Solution {
    public int[][] intervalIntersection(int[][] firstList, int[][] secondList) {
        List<int[]> res = new ArrayList<>();
        int idxFir = 0;
        int idxSec = 0;
        while(idxFir<firstList.length && idxSec<secondList.length){
            int left = Math.max(firstList[idxFir][0],secondList[idxSec][0]);
            int right = Math.min(firstList[idxFir][1],secondList[idxSec][1]);
            if(left<=right){
                res.add(new int[]{left,right});   
            }
            
            if(secondList[idxSec][1] > firstList[idxFir][1]){
                idxFir++;
            }
            else{
                idxSec++;
            }
        }
        return res.toArray(new int[res.size()][]);
    }
}
```