题目:

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

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

样例

插入区间[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. css style与class之间的区别

    问题描述:    网页点击[导出]按钮后,将页面table内容另存成excel文件,却发现无法保存表格样式 分析过程: 1.table表格用class,而不是style.导出时并没有导出class定义 ...

  2. JS重写alert,保证弹窗错误的友好性

    // ------------------------------------------------------------- // 重写alert,保证弹窗错误的友好性 var j_oldAler ...

  3. php 文件上传简单类---限制仅上传jpg文件

    php 文件上传代码,限制只能上传jpg格式文件,也可以自行添加其它扩展名的文件. <?php /* * 图片上传类 仅限JPG格式图片 * edit by www.jbxue.com at 2 ...

  4. 工厂方法模式与IoC/DI控制反转和依赖注入

    IoC——Inversion of Control  控制反转 DI——Dependency Injection   依赖注入 要想理解上面两个概念,就必须搞清楚如下的问题: 参与者都有谁? 依赖:谁 ...

  5. sqlServer2008 手工注入

    接着上一篇的<mysql手工注入> 参考:http://hi.baidu.com/ciqing_s/item/971bf994365130accc80e5ed http://hi.baid ...

  6. hdu 1222 狼和兔子

    Description There is a hill with n holes around. The holes are signed from 0 to n-1. A rabbit must h ...

  7. oracle-linux下挂载"移动硬盘" NTFS类型

    环境: ORACLE-LINUX 5.7 全新移动硬盘(未使用过) 移动硬盘空间3T 在默认情况下,Linux系统不支持NTFS分区挂载 1.服务器: A服务器和B服务器为一套ORACLE-RAC,移 ...

  8. WPF-控件-ListView

    <Window x:Class="DataTemplate2.MainWindow" xmlns="http://schemas.microsoft.com/win ...

  9. ios上取得设备唯一标志的解决方案

    iOS 7中苹果再一次无情的封杀mac地址,现在已经不能获取ios7设备的物理地址.那么在开发中如何才能标识设备的唯一性呢?apple公司提供的方法是通过keychain来存一些标志信息,然后通过存的 ...

  10. ASP.NET 运行机制续(完结)

    上一篇说到applicationInstance会执行一些列的事件.下面是我在msdn上找到有关asp.net程序生命周期相关的描述及图片 声明周期的起始 ASP.NET 应用程序的生命周期以浏览器向 ...