Insert Interval 



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.



Example 1:

Given intervals [1,3],[6,9], insert and merge [2,5] in as [1,5],[6,9].



Example 2:

Given [1,2],[3,5],[6,7],[8,10],[12,16], insert and merge [4,9] in as [1,2],[3,10],[12,16].

This is because the new interval [4,9] overlaps with [3,5],[6,7],[8,10].

思路:插入合并就可以,相比于上一题。这题还要简单一些。

详细代码例如以下:

/**
* 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; }
* }
*/
public class Solution {
public List<Interval> insert(List<Interval> intervals, Interval newInterval) {
List<Interval> list = new ArrayList<Interval>();
//边界情况
if(intervals.size() == 0){
list.add(newInterval);
return list;
}
//循环推断
for(int i = 0; i < intervals.size();i++){
//假设新的区间结束值小于開始值。则直接插入前面。后面依次插入就可以
if(newInterval.end < intervals.get(i).start){
list.add(newInterval);
for(int j = i; j < intervals.size(); j++){
list.add(intervals.get(j));
}
break;
}
//新的区间開始点大于结束点。则当前点直接加入结果集
else if(newInterval.start > intervals.get(i).end){
list.add(intervals.get(i));
}
//须要合并的情况
else{
//合并区间
newInterval.start = Math.min(newInterval.start,intervals.get(i).start);
newInterval.end = Math.max(newInterval.end,intervals.get(i).end);
}
if(i == intervals.size() - 1){//假设是最后一个数据。也加入结果集中
list.add(newInterval);
}
}
return list;
}
}

leetCode 57.Insert Interval (插入区间) 解题思路和方法的更多相关文章

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

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

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

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

  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(Hard)

    1. 原题链接 https://leetcode.com/problems/insert-interval/description/ 2. 题目要求 该题与上一题的区别在于,插入一个新的interva ...

  6. 第一周 Leetcode 57. Insert Interval (HARD)

    Insert interval  题意简述:给定若干个数轴上的闭区间,保证互不重合且有序,要求插入一个新的区间,并返回新的区间集合,保证有序且互不重合. 只想到了一个线性的解法,所有区间端点,只要被其 ...

  7. leetcode 57 Insert Interval & leetcode 1046 Last Stone Weight & leetcode 1047 Remove All Adjacent Duplicates in String & leetcode 56 Merge Interval

    lc57 Insert Interval 仔细分析题目,发现我们只需要处理那些与插入interval重叠的interval即可,换句话说,那些end早于插入start以及start晚于插入end的in ...

  8. [LeetCode] Insert Interval 插入区间

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

  9. 057 Insert Interval 插入区间

    给出一个无重叠的按照区间起始端点排序的区间列表.在列表中插入一个新的区间,你要确保列表中的区间仍然有序且不重叠(如果有必要的话,可以合并区间).示例 1:给定区间 [1,3],[6,9],插入并合并 ...

随机推荐

  1. 阿里云域名绑定IP

    前提条件:拥有一个阿里云域名,拥有一台自己的服务器,并且知道ip,我的是nginx 1.登陆阿里云https://www.aliyun.com/ 2.选择域名与网站,会看到自己拥有的域名,比如我的是m ...

  2. window命令

    查看端口占用命令: 开始--运行--cmd 进入命令提示符 输入netstat -aon 即可看到所有连接的PID 之后在任务管理器中找到这个PID所对应的程序如果任务管理器中没有PID这一项,可以在 ...

  3. 2017.4.18 静态代码分析工具sonarqube+sonar-runner的安装配置及使用

    配置成功后的代码分析页面: 可以看到对复杂度.语法使用.重复度等等都做了分析,具体到了每一个方法和每一句代码. 四种使用方式: sonarqube + sonar-runner sonarqube + ...

  4. [Functional Programming] Combine State Dependent Transactions with the State ADT (composeK to replace multi chian call)

    When developing a Finite State Machine, it is often necessary to apply multiple transitions in tande ...

  5. [转载]LoadRunner如何处理AJAX异步请求

    最近在网上经常有人问“LoadRunner脚本回放成功,但数据没有写入数据库,这是什么原因”,记得以前的同事也遇到过相同的问题,再次将解决方法贴出来,希望能帮助大家. 相信大家在做测试的过程中,特别是 ...

  6. Linux Oracle数据库的安装

    // 注释 # root用户 $oracle用户 1. 关闭安全措施    # chkconfig iptables off // 永久关闭防火墙 # serviceiptables stop // ...

  7. 为什么JVM指定-Xmx参数后占用内存会变少?

    嘿,你能顺便过来看看这个奇怪的事情吗?” 就是让我提供支持的这个事情,驱使我写下这篇博客的.这个特殊的问题是,不同工具给出的可用内存的报告是不一样的. 简而言之,工程师正在调查特定应用程序的内存使用. ...

  8. Execution failed for task ':app:transformClassesAndResourcesWithProguardForRelease'.

    1.Execution failed for task ':app:transformClassesAndResourcesWithProguardForRelease'. 原因解析: gradle ...

  9. js 获取浏览器显示内容的宽度和高度

      js获取浏览器显示内容的宽度和高度 CreateTime--2017年7月10日17:24:12Author:Marydon 1.获取浏览器屏幕显示d的网页宽度 /** * 得到浏览器显示的屏幕高 ...

  10. 远程链接mysql数据库

    mysql -P3306 -uroot -proot 显示最大连接数 show variables like '%max_connections%'; 设置最大链接数 ;//默认100--只对当前进程 ...