题目:

给出一个无重叠的按照区间起始端点排序的区间列表。

在列表中插入一个新的区间,你要确保列表中的区间仍然有序且不重叠(如果有必要的话,可以合并区间)。

样例

插入区间[2, 5] 到 [[1,2], [5,9]],我们得到 [[1,9]]

插入区间[3, 4] 到 [[1,2], [5,9]],我们得到 [[1,2], [3,4], [5,9]]

解题:

参考合并区间思想,可以将插入区间插入到列表中,再按照合并区间的思想解题

class Solution {
/**
* Insert newInterval into intervals.
* @param intervals: Sorted interval list.
* @param newInterval: A new interval.
* @return: A new sorted interval list.
*/
public ArrayList<Interval> insert(ArrayList<Interval> intervals, Interval newInterval) {
ArrayList<Interval> result = new ArrayList<Interval>();
if(intervals == null || intervals.size() <=0){
result.add(newInterval);
return result;
}
intervals.add(newInterval);
Collections.sort(intervals,new IntervalComparator());
Interval last = intervals.get(0);
for(int i = 1;i<intervals.size();i++){
Interval cur = intervals.get(i);
if(last.end>= cur.start){
last.end = Math.max(last.end,cur.end);
}else{
result.add(last);
last = cur;
}
}
result.add(last);
return result;
}
private class IntervalComparator implements Comparator<Interval>{
public int compare(Interval a,Interval b){
return a.start - b.start;
}
}
}

原始已经有序,新加入元素后重新排序,没有这个必要了。

/**
* Definition of Interval:
* public classs Interval {
* int start, end;
* Interval(int start, int end) {
* this.start = start;
* this.end = end;
* }
*/ class Solution {
/**
* Insert newInterval into intervals.
* @param intervals: Sorted interval list.
* @param newInterval: A new interval.
* @return: A new sorted interval list.
*/ public ArrayList<Interval> insert(ArrayList<Interval> intervals, Interval newInterval) {
ArrayList<Interval> result = new ArrayList<Interval>();
// write your code here
if(intervals==null ||intervals.size()<=0){
result.add(newInterval);
return result;
}
int insertPos = 0;
for(int i=0;i<intervals.size();i++){
Interval curt = intervals.get(i);
if(curt.end<newInterval.start){// 不相交 cur 后插入新区间
result.add(curt);
insertPos++;
} else if(curt.start>newInterval.end){// 不相交 cur 前插入新区间
result.add(curt);
}else{
newInterval.start = Math.min(curt.start,newInterval.start);
newInterval.end = Math.max(curt.end,newInterval.end);
} }
result.add(insertPos,newInterval);
return result;
}
}

参考:http://www.jiuzhang.com/solutions/insert-interval/

lintcode:插入区间的更多相关文章

  1. LintCode 30插入区间

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

  2. 【BZOJ】3065: 带插入区间K小值

    http://www.lydsy.com/JudgeOnline/problem.php?id=3065 题意:带插入.修改的区间k小值在线查询.(原序列n<=35000, 询问<=175 ...

  3. bzoj 3065: 带插入区间K小值 替罪羊树 && AC300

    3065: 带插入区间K小值 Time Limit: 60 Sec  Memory Limit: 512 MBSubmit: 1062  Solved: 253[Submit][Status] Des ...

  4. LeetCode 57 插入区间

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

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

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

  6. [BZOJ3065]带插入区间K小值 解题报告 替罪羊树+值域线段树

    刚了一天的题终于切掉了,数据结构题的代码真**难调,这是我做过的第一道树套树题,做完后感觉对树套树都有阴影了......下面写一下做题记录. Portal Gun:[BZOJ3065]带插入区间k小值 ...

  7. 【题解】BZOJ 3065: 带插入区间K小值——替罪羊树套线段树

    题目传送门 题解 orz vfk的题解 3065: 带插入区间K小值 系列题解 一 二 三 四 惨 一开始用了一种空间常数很大的方法,每次重构的时候merge两颗线段树,然后无限RE(其实是MLE). ...

  8. 【BZOJ3065】带插入区间K小值 替罪羊树+权值线段树

    [BZOJ3065]带插入区间K小值 Description 从前有n只跳蚤排成一行做早操,每只跳蚤都有自己的一个弹跳力a[i].跳蚤国王看着这些跳蚤国欣欣向荣的情景,感到非常高兴.这时跳蚤国王决定理 ...

  9. 3065: 带插入区间K小值_树套树_替罪羊树_权值线段树

    经过周六一天,周一3个小时的晚自习,周二2个小时的疯狂debug,终于凭借自己切掉了这道树套树题. Code: #include <cstdio> #include <algorit ...

随机推荐

  1. 了解jsonp

    <script>                //创建全局函数,用来处理 跨域 获取到的信息:        function name(data){            ....   ...

  2. TortoiseGit 安装和使用的图文教程

    TortoiseGit.SourceTree都是Windows下不错的Git客户端工具,下面介绍一下TortoiseGit安装和使用的方法. 安装TortoiseGit并使用它需要两个软件:Torto ...

  3. 基于jquery打造的网页右侧自动收缩浮动在线客服代码

    基于jquery打造的网页右侧自动收缩浮动在线QQ客服代码, 当前比较流行的一款QQ在线jquery特效代码, 代码中还带有IE6下PNG图片透明的特效,如果想研究IE6下PNG透明的同学也可以下载研 ...

  4. JAVA 编码机制

    先看例子: public class Test { public static void main(String[] args) { char han = '永'; System.out.printl ...

  5. 如何在ARC代码中混编非ARC代码

    “ios中如果arc和非arc文件混编,可以在build parses中指定compile flags,如果arc文件设为"-fobjc-arc",非arc文件设为"-f ...

  6. Oracle 10g下载链接

    用迅雷下载: http://download.oracle.com/otn/linux/oracle10g/10201/10201_database_linux_x86_64.cpio.gz http ...

  7. LINQ.CS

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Zdso ...

  8. Install mcrypt for php on Mac OSX 10.10 Yosemite for a Development Server

    mcrypt is a file encryption method using secure techniques to exchange data. It is required for some ...

  9. Linq to XML 之XElement的Descendants方法的新发现

    C#操作XML的方法有很多,但个人认为最方便的莫过于Linq to XML了,特别是XElement的Descendants方法是我最常用的一个方法. 这个方法可以根据节点名(Name)找到当前调用的 ...

  10. WebAPi(selfhost)

    ASP.NET WebAPi(selfhost)之文件同步或异步上传   前言 前面我们讲过利用AngularJs上传到WebAPi中进行处理,同时我们在MVC系列中讲过文件上传,本文结合MVC+We ...