美文网首页数据结构与算法
Leetcode 986 区间列表的交集

Leetcode 986 区间列表的交集

作者: itbird01 | 来源:发表于2021-12-22 07:15 被阅读0次
题目.png

题意:给定两个由一些 闭区间 组成的列表,firstList 和 secondList ,其中 firstList[i] = [starti, endi] 而 secondList[j] = [startj, endj] 。每个区间列表都是成对 不相交 的,并且 已经排序 。
返回这 两个区间列表的交集 。
形式上,闭区间 [a, b](其中 a <= b)表示实数 x 的集合,而 a <= x <= b 。
两个闭区间的 交集 是一组实数,要么为空集,要么为闭区间。例如,[1, 3] 和 [2, 4] 的交集为 [2, 3] 。

解题思路

解法1:
1.首先分析题意,我们知道两个区间相交,有三种情况,如图:


三种情况.png

2.由题意分析可知,区间A和区间B所在的数组,是排序数组,接下来我们利用这一信息分别分析这三种情况
1)第一种情况,我们把相交区域,放入结果数组,区间A的左端此时就没有用了,所以区间A所在的数组应该移到的下一个区间,区间B的左端已经进入结果数组,所以区间B的区间应该更新start位置startb=enda
2)第二种情况,我们把相交区域,放入结果数组,区间B此时已全部放入结果数组,所以区间B所在的数组应该移动到下一个区间;区间A的左端应该舍弃,所以区间A更新start位置starta=endb
3)第三种情况,此时没有相交区域,区间A可以整个舍弃,区间A所在的数组应该移动到下一个区间;区间B不动
分析情况如图:

image.png

解题遇到的问题

后续需要总结学习的知识点

需要深入总结学习二叉搜索树的数据结构的特点、应用、搜索、插入、删除

##解法1
import java.util.ArrayList;

class Solution {
    public int[][] intervalIntersection(int[][] firstList, int[][] secondList) {
        int starta = 0, enda = 0, startb = 0, endb = 0;
        int i = 0, j = 0;
        ArrayList<Integer> start = new ArrayList<Integer>();
        ArrayList<Integer> end = new ArrayList<Integer>();

        while (i < firstList.length && j < secondList.length) {
            starta = firstList[i][0];
            enda = firstList[i][1];
            startb = secondList[j][0];
            endb = secondList[j][1];
            if (starta < startb) {
                if (enda < startb) {
                    i++;
                } else {
                    if (endb < enda) {
                        start.add(startb);
                        end.add(endb);
                        firstList[i][0] = endb;
                        j++;
                    } else {
                        start.add(startb);
                        end.add(enda);
                        secondList[j][0] = enda;
                        i++;
                    }
                }
            } else {
                if (endb < starta) {
                    j++;
                } else {
                    if (enda < endb) {
                        start.add(starta);
                        end.add(enda);
                        secondList[j][0] = enda;
                        i++;
                    } else {
                        start.add(starta);
                        end.add(endb);
                        firstList[i][0] = endb;
                        j++;
                    }
                }
            }
        }

        int[][] ans = new int[start.size()][2];
        for (int k = 0; k < ans.length; k++) {
            ans[k][0] = start.get(k);
            ans[k][1] = end.get(k);
        }
        return ans;
    }
}

相关文章

  • Leetcode 986 区间列表的交集

    题意:给定两个由一些 闭区间 组成的列表,firstList 和 secondList ,其中 firstList...

  • 区间调度之区间交集问题

    读完本文,你可以去力扣拿下如下题目: 986.区间列表的交集[https://leetcode-cn.com/pr...

  • LeetCode #986 Interval List Inte

    986 Interval List Intersections 区间列表的交集 Description:You a...

  • 986. 区间列表的交集(Python)

    难度:★★★☆☆类型:数组方法:逻辑 题目 力扣链接请移步本题传送门[https://leetcode-cn.co...

  • JavaScript - 区间列表交集(双指针法)

    给定两个由一些 闭区间 组成的列表,每个区间列表都是成对不相交的,并且已经排序,返回这两个区间列表的交集。示例:输...

  • [LeetCode]57、插入区间

    题目描述 给出一个无重叠的 ,按照区间起始端点排序的区间列表。 在列表中插入一个新的区间,你需要确保列表中的区间仍...

  • Leetcode【368、986】

    问题描述:【DP】368. Largest Divisible Subset 解题思路: 最大可除子集。给一个包含...

  • LeetCode 56 [Merge Intervals]

    原题 给出若干闭合区间,合并所有重叠的部分。 样例给出的区间列表 => 合并后的区间列表: 解题思路 首先,把区间...

  • lintcode 插入空间

    给出一个无重叠的按照区间起始端点排序的区间列表。在列表中插入一个新的区间,你要确保列表中的区间仍然有序且不重叠(如...

  • LeetCode 57 [Insert Interval]

    原题 给出一个无重叠的按照区间起始端点排序的区间列表。在列表中插入一个新的区间,你要确保列表中的区间仍然有序且不重...

网友评论

    本文标题:Leetcode 986 区间列表的交集

    本文链接:https://www.haomeiwen.com/subject/vtsxqrtx.html