给出一个无重叠的按照区间起始端点排序的区间列表。
在列表中插入一个新的区间,你要确保列表中的区间仍然有序且不重叠(如果有必要的话,可以合并区间)。
示例 1:
给定区间 [1,3],[6,9],插入并合并 [2,5] 得到 [1,5],[6,9].
示例 2:
给定区间 [1,2],[3,5],[6,7],[8,10],[12,16],插入并合并 [4,9] 得到 [1,2],[3,10],[12,16].
这是因为新的区间 [4,9] 与 [3,5],[6,7],[8,10] 重叠。
详见:https://leetcode.com/problems/insert-interval/description/

Java实现:

/**
* Definition for an interval.
* public class Interval {
* int start;
* int end;
* Interval() { start = 0; end = 0; }
* Interval(int s, int e) { start = s; end = e; }
* }
*/
class Solution {
public List<Interval> insert(List<Interval> intervals, Interval newInterval) {
List<Interval> res = new ArrayList<Interval>(); for(Interval each: intervals){
if(each.end < newInterval.start){
res.add(each);
}else if(each.start > newInterval.end){
res.add(newInterval);
newInterval = each;
}else if(each.end >= newInterval.start || each.start <= newInterval.end){
int nstart = Math.min(each.start, newInterval.start);
int nend = Math.max(newInterval.end, each.end);
newInterval = new Interval(nstart, nend);
}
}
res.add(newInterval);
return res;
}
}

参考:https://www.cnblogs.com/springfor/p/3872333.html

057 Insert Interval 插入区间的更多相关文章

  1. leetCode 57.Insert Interval (插入区间) 解题思路和方法

    Insert Interval  Given a set of non-overlapping intervals, insert a new interval into the intervals ...

  2. [LeetCode] Insert Interval 插入区间

    Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...

  3. [leetcode]57. Insert Interval插入区间

    Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...

  4. [LeetCode] 57. Insert Interval 插入区间

    Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...

  5. LeetCode 57. Insert Interval 插入区间 (C++/Java)

    题目: Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if nec ...

  6. Java for LeetCode 057 Insert Interval

    Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...

  7. leetcode Insert Interval 区间插入

    作者:jostree  转载请注明出处 http://www.cnblogs.com/jostree/p/4051169.html 题目链接:leetcode Insert Interval 使用模拟 ...

  8. [Swift]LeetCode57. 插入区间 | Insert Interval

    Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...

  9. 合并区间 · Merge Intervals & 插入区间 · Insert Interval

    [抄题]: 给出若干闭合区间,合并所有重叠的部分. 给出的区间列表 => 合并后的区间列表: [ [ [1, 3], [1, 6], [2, 6], => [8, 10], [8, 10] ...

随机推荐

  1. CF785CAnton and Permutation(分块 动态逆序对)

    Anton likes permutations, especially he likes to permute their elements. Note that a permutation of  ...

  2. 7th

    2017-2018-2 20179212<网络攻防技术>第7周作业 课本学习 Windows操作系统基本框架 1.windows基本结构分为运行于处理器特权模式的操作系统内核以及运行在处理 ...

  3. hdfs 查看报告--命令(hdfs dfsadmin -report)

    [hadoop@master sbin]$ hdfs dfsadmin -reportConfigured Capacity: 8202977280 (7.64 GB)Present Capacity ...

  4. 基于django封装的常用装饰器和函数

    1:返回操作成功的json数据 def response_success(message, data=None, data_list=[]): return HttpResponse(json.dum ...

  5. BZOJ3295:[CQOI2011]动态逆序对

    浅谈树状数组与线段树:https://www.cnblogs.com/AKMer/p/9946944.html 题目传送门:https://www.lydsy.com/JudgeOnline/prob ...

  6. springboot 1.5.x中的动态切换日志级别

    logback是一套日志框架,由log4j的优化版,由同一个作者开发,在速度和性能上都超过其他日志框架,再结合slf4j,已成为当前最流行的日志框架. 一.springboot中使用logback s ...

  7. algo: 冒泡排序(Java实现)

    package com.liuxian.algo; public class MySortClass implements Comparable<MySortClass> { public ...

  8. Thinkpad 拆光驱更换光驱硬盘支架、拆光驱面板 T400 T440

    拆光驱.硬盘装支架的环节就不多说了.主要说下拆光驱面板. 先拿细物(区别针.回形针),捅这个洞,就能把光驱仓打开弹出来后,反过来,这里有个卡扣放大看,按住这卡扣,然后往外掰,把面板掰出来 掰出来的面板 ...

  9. CSP 的有用资料

    具体请参考: http://software-security.sans.org/downloads/appsec-2014-files/building-a-content-security-pol ...

  10. Guid.NewGuid().ToString()的几种格式 (转)

    1.Guid.NewGuid().ToString("N") 结果为:      38bddf48f43c48588e0d78761eaa1ce6 2.Guid.NewGuid() ...