资讯专栏INFORMATION COLUMN

57. Insert Interval

kid143 / 2096人阅读

57. Insert Interval

题目链接:https://leetcode.com/problems...

public class Solution {
    public List insert(List intervals, Interval newInterval) {
        // skip intervals which not overlap with new one 
        // intervals.get(i).end < newInterval.start
        List res = new ArrayList();
        int i = 0, n = intervals.size();
        while(i < n && intervals.get(i).end < newInterval.start) res.add(intervals.get(i++));
        // merge the overlap part
        // intervals.get(i).start <= newInterval.end
        while(i < n && intervals.get(i).start <= newInterval.end) {
            newInterval.start = Math.min(newInterval.start, intervals.get(i).start);
            newInterval.end = Math.max(newInterval.end, intervals.get(i).end);
            i++;
        }
        res.add(newInterval);
        // add last intervals without overlap with new one
        while(i < n) res.add(intervals.get(i++));
        
        return res;
    }
}

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/69887.html

相关文章

  • leetcode--57--Insert Interval

    摘要:问题描述分析这道题的关键在于理解问题,抽取原型,理解中间可以部分如何界定,以及非部分如何进行追加。需要注意的是循环到最后一个元素和在最后一个元素的区别。 问题描述: Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary). You m...

    kycool 评论0 收藏0
  • leetcode57. Insert Interval

    摘要:题目要求给定一组顺序排列且相互之间没有重叠的区间,输入一个区间,将它插入到当前的区间数组中,并且将需要合并的区间合并,之后返回插入并且合并后的区间。我们将这三个类型的区间分别标注为类型,类型,类型。 题目要求 Given a set of non-overlapping intervals, insert a new interval into the intervals (merge...

    Yuanf 评论0 收藏0
  • [Leetcode] Merge Intervals and Insert Interval 合并间

    摘要:我们只要把所有和该有重叠的合并到一起就行了。最后把前半部分的列表,合并后的大和后半部分的列表连起来,就是结果了。 Merge Intervals 最新更新请见 https://yanjia.me/zh/2019/02/... Given a collection of intervals, merge all overlapping intervals.For example, Gi...

    antyiwei 评论0 收藏0
  • MySQL 快速创建千万级测试数据

    摘要:备注此文章的数据量在,如果想要千万级,调大数量即可但是不要大量使用或者会导致性能下降背景在进行查询操作的性能测试或者优化时,我们经常需要在线下环境构建大量的基础数据供我们测试,模拟线上的真实环境。备注: 此文章的数据量在100W,如果想要千万级,调大数量即可,但是不要大量使用rand() 或者uuid() 会导致性能下降 背景 在进行查询操作的性能测试或者sql优化时,我们经常需要在线下环境...

    李文鹏 评论0 收藏0
  • [LeetCode] Insert Interval

    Problem Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary). You may assume that the intervals were initially sorted according to their start times....

    Jonathan Shieber 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<