Description

Given a non-overlapping interval list which is sorted by start point.

Insert a new interval into it, make sure the list is still in order and non-overlapping (merge intervals if necessary).

Example

Insert (2, 5) into [(1,2), (5,9)], we get [(1,9)].

Insert (3, 4) into [(1,2), (5,9)], we get [(1,2), (3,4), (5,9)].

题意:给定一个区间,将它插进一个有序的区间集合里,新的区间依然要保持有序性。这就需要考虑到区间的合并问题,我们可以定义一个新的集合,来存放最后的结果。定义一个temp游标,用一个循环从旧的集合中依次取出区间,与待插入区间进行比较。那么如何比较呢?假定新区间的end都小于temp的start,那说明新区间比temp要小,那么直接将新区间放进结果集合里就行了,剩下的依次插入。不然,则说明需要进行区间的合并,具体代码如下:

/**
* Definition of Interval:
* public classs Interval {
* int start, end;
* Interval(int start, int end) {
* this.start = start;
* this.end = end;
* }
* }
*/ public class Solution {
/**
* @param intervals: Sorted interval list.
* @param newInterval: new interval.
* @return: A new interval list.
*/
public List<Interval> insert(List<Interval> intervals, Interval newInterval) {
// write your code here
//特殊情况的讨论
List<Interval>ans=new ArrayList<Interval>();
if(intervals.size()==0){
ans.add(newInterval);
return ans;
}
if(newInterval==null){
return intervals;
}
if(newInterval.start>intervals.get(intervals.size()-1).end){
intervals.add(newInterval);
return intervals;
} //一般情况的讨论
Interval last=null;
for(int i=0;i<intervals.size();i++){
//用不到newIneval
Interval temp=intervals.get(i);
if(newInterval.start>temp.end){
ans.add(temp);
continue;
}else{
//分两种情况
if(newInterval.end<temp.start){
ans.add(newInterval);
last=temp;
}else{
int start=newInterval.start<temp.start?newInterval.start:temp.start;
int end=newInterval.end<temp.end?temp.end:newInterval.end;
//合并
last=new Interval(start,end);
}
//对剩下的进行处理
for(int j=i+1;j<intervals.size();j++){
Interval t=intervals.get(j);
if(last.end<t.start){
//归并完成
ans.add(last);
last=t;
}else{
//继续归并
last.end=last.end>t.end?last.end:t.end;
}
}
ans.add(last);
break;
}
}
return ans;
}
}

30. Insert Interval【LintCode by java】的更多相关文章

  1. 156. Merge Intervals【LintCode by java】

    Description Given a collection of intervals, merge all overlapping intervals. Example Given interval ...

  2. 212. Space Replacement【LintCode by java】

    Description Write a method to replace all spaces in a string with %20. The string is given in a char ...

  3. 165. Merge Two Sorted Lists【LintCode by java】

    Description Merge two sorted (ascending) linked lists and return it as a new sorted list. The new so ...

  4. 158. Valid Anagram【LintCode by java】

    Description Write a method anagram(s,t) to decide if two strings are anagrams or not. Clarification ...

  5. 177. Convert Sorted Array to Binary Search Tree With Minimal Height【LintCode by java】

    Description Given a sorted (increasing order) array, Convert it to create a binary tree with minimal ...

  6. 173. Insertion Sort List【LintCode by java】

    Description Sort a linked list using insertion sort. Example Given 1->3->2->0->null, ret ...

  7. 172. Remove Element【LintCode by java】

    Description Given an array and a value, remove all occurrences of that value in place and return the ...

  8. 155. Minimum Depth of Binary Tree【LintCode by java】

    Description Given a binary tree, find its minimum depth. The minimum depth is the number of nodes al ...

  9. 211. String Permutation【LintCode by java】

    Description Given two strings, write a method to decide if one is a permutation of the other. Exampl ...

随机推荐

  1. miniblast_hash算法c语言实现

    对于一组基因文件中的基因序列,选取一段基因片段,作为索引,利用hash表,查找固定的基因片段.有一定的并且容忍错误. 简单讲就是自己实现一个hashtable,将选出特定字符串建立索引,便于查询.输出 ...

  2. java.lang.UnsatisfiedLinkError: /usr/openv/java/jre/lib/amd64/libawt_xawt.so: libXtst.so.6: cannot open shared object file: No such file or directory

    解决办法: 在radhat 或者centos系统中运行一下命令即可:yum install libXext.x86_64yum install libXrender.x86_64yum install ...

  3. WebDriverException: Message: A session is either terminated or not started

    错误提示: …… [debug] [XCUITest] Connection to WDA timed out[debug] [iProxy] recv failed: Operation not p ...

  4. Android Studio Git .gitignore文件配置忽略不需要的文件

    转载请标明出处: http://www.cnblogs.com/why168888/p/6440805.html 本文出自:[Edwin博客园] # Built application files * ...

  5. WebSocket消息推送

    WebSocket协议是基于TCP的一种新的网络协议,应用层,是TCP/IP协议的子集. 它实现了浏览器与服务器全双工(full-duplex)通信,客户端和服务器都可以向对方主动发送和接收数据.在J ...

  6. c++ 一般虚函数

    类图: 代码: #include <iostream> using namespace std; class CFather //父类 { public: virtual void dis ...

  7. Why Reactive(Cocoa)?-时间线、输入、输出、复杂性、异步、状态、聚合

    To put it another way, the output at any one time is the result of combining all inputs. The output ...

  8. P2585 [ZJOI2006]三色二叉树

    题目描述 输入输出格式 输入格式: 输入文件名:TRO.IN 输入文件仅有一行,不超过500000个字符,表示一个二叉树序列. 输出格式: 输出文件名:TRO.OUT 输出文件也只有一行,包含两个数, ...

  9. Markdown语法初体验

    前言 由于把博客主题样式换了,所以改用Markdown语法,让代码看起来更加舒服一些. 照葫芦画瓢 这里是H1标题(===) 这里是H2标题(---) 使用一个#号 使用两个#号 使用三个#号 引用 ...

  10. NIO高并发基础

    NIO高并发 是jdk1.4出现的新的流. NIO - New IO - 同步式非阻塞式IO BIO - Blocking IO - 同步式阻塞式IO ---UDP/TCP ==AIO - Async ...