LeetCode 986. 区间列表的交集

166 阅读1分钟

986. 区间列表的交集

难度 中等

给定两个由一些 闭区间 组成的列表,firstListsecondList ,其中 firstList[i] = [starti, endi]secondList[j] = [startj, endj] 。每个区间列表都是成对 不相交 的,并且 已经排序

返回这 两个区间列表的交集

形式上,闭区间 [a, b](其中 a <= b)表示实数 x 的集合,而 a <= x <= b

两个闭区间的 交集 是一组实数,要么为空集,要么为闭区间。例如,[1, 3][2, 4] 的交集为 [2, 3]

示例 1:

img

输入:firstList = [[0,2],[5,10],[13,23],[24,25]], secondList = [[1,5],[8,12],[15,24],[25,26]]
输出:[[1,2],[5,5],[8,10],[15,23],[24,24],[25,25]]

示例 2:

输入:firstList = [[1,3],[5,9]], secondList = []
输出:[]

示例 3:

输入:firstList = [], secondList = [[4,8],[10,12]]
输出:[]

示例 4:

输入:firstList = [[1,7]], secondList = [[3,10]]
输出:[[3,7]]

提示:

  • 0 <= firstList.length, secondList.length <= 1000
  • firstList.length + secondList.length >= 1
  • 0 <= starti < endi <= 109
  • endi < starti+1
  • 0 <= startj < endj <= 109
  • endj < startj+1

题解

这道题是求所给区间的交集,刚开始想法就是遍历+判断,通过判断区间起始和结束之间的大小关系确定添不添加。

解法一

通过指针+判断进行求交集,有以下6种情况,设置两个辅助指针i和J:

  • 如果b_j_0 > a_i_1,b区间最小的元素都大于a区间最大的元素,跳过这个a区间,i++
  • 如果a_i_0 > b_j_1,a区间最小的元素都大于b区间最大的元素,跳过这个b区间,j++
  • 如果a_i_0 > b_j_0,且a_i_1 < b_j_1,a区间在b区间内,直接取整个a区间,i++
  • 如果a_i_0 > b_j_0,且a_i_1 > b_j_1,a区间小的部分和b区间大的部分重叠,取a_i_0到b_j_1区间,j++
  • 如果b_j_0 > a_i_0,且b_j_1 < a_i_1,b区间在a区间内,直接取整个b区间,j++
  • 如果b_j_0 > a_i_0,且b_j_1 > a_i_1,b区间小的部分和a区间大的部分重叠,取b_j_0到a_i_1区间,i++
class Solution {
    public int[][] intervalIntersection(int[][] a, int[][] b) {
        int alen = a.length;
        int blen = b.length;
        int i = 0;
        int j = 0;
        List<int[]> res = new ArrayList<int[]>();
        while(i < alen && j < blen){
            int oldi = -1;
            int oldj = -1;
            while(i != oldi || j !=oldj){
                oldi = i;
                oldj = j;
                while(i < alen && j < blen && b[j][0] > a[i][1]){//b区间最小的元素都大于a区间最大的元素,跳过这个a区间
                    i++;
                }
                while(i < alen && j < blen && a[i][0] > b[j][1]){//a区间最小的元素都大于b区间最大的元素,跳过这个b区间
                    j++;
                }
            }
            if(i < alen && j < blen){
                if(a[i][0] >= b[j][0]){
                    if(a[i][1] <= b[j][1]){//a区间在b区间内,直接取整个a区间
                        res.add(new int[]{a[i][0], a[i][1]});
                        i++;
                    }else{//a区间小的部分和b区间大的部分重叠,取a_i_0到b_j_1区间
                        res.add(new int[]{a[i][0], b[j][1]});
                        j++;
                    }
                }else if(b[j][0] >= a[i][0]){
                    if(b[j][1] <= a[i][1]){//b区间在a区间内,直接取整个b区间
                        res.add(new int[]{b[j][0], b[j][1]});
                        j++;
                    }else{//b区间小的部分和a区间大的部分重叠,取b_j_0到a_i_1区间
                        res.add(new int[]{b[j][0], a[i][1]});
                        i++;
                    }
                }
            }
        }
        return res.toArray(new int[res.size()][2]);
    }
}

解法二

看到题解我是懵的,这也行,仔细验证了一下,没有出现错误,真的太秒了。

class Solution {
    public int[][] intervalIntersection(int[][] A, int[][] B) {
        List<int []> ans = new ArrayList();
        int i = 0, j = 0;
        while(i < A.length && j < B.length){
            int lo = Math.max(A[i][0], B[j][0]);//看看AB区间小的部分哪个大,因为两个区间只有大的部分重叠,小的部分另外一个没有
            int hi = Math.min(A[i][1], B[j][1]);//看看AB区间大的部分哪个小,因为两个区间只有小的部分重叠,大的部分另外一个没有
            if(hi >= lo){//如果大的部分大于等于小的部分,有重叠;否则不存再重叠
                ans.add(new int[]{lo, hi});
            }
​
            if(A[i][1] < B[j][1]){//小的区间已经全部遍历完了,移动到下一个区间
                i++;
            }else{
                j++;
            }
        }
        return ans.toArray(new int[ans.size()][]);
    }
}