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. Java学习---XML的读写操作

    DOM4_Jwriter.java package com.ftl.xmlparse; import java.io.File; import java.io.FileNotFoundExceptio ...

  2. EXCHANGE 2013 队列

    每当咱在Exchange里查看队列的时候,我们会看到队列分成好几个组,每个邮箱数据库都有自己的目标队列,DAG.AD站点也是,AD林也是一个队列,最后最多的就是外部SMTP域队列. 当传输服务处理队列 ...

  3. 微软最新设计Fluent Design System初体验

    微软最新设计Fluent Design System初体验 本文图片不全!建议移步知乎专栏查看!!! https://zhuanlan.zhihu.com/p/30582886 原创 2017-11- ...

  4. ZT A2DP协议笔记

    A2DP协议笔记 (2013-07-30 10:07:54) 转载▼ 标签: a2dp bluetooth src sink 分类: Bluetooth 1.概述     A2DP(Advanced ...

  5. [NOIP 2011] 聪明的质检员

    聪明的质检员 描述 小 T 是一名质量监督员,最近负责检验一批矿产的质量.这批矿产共有n个矿石,从1到n逐一编号,每个矿石都有自己的重量wi以及价值vi.检验矿产的流程是:1.给定m个区间[Li,Ri ...

  6. 【转】iphone 输入/输出流异步读写数据

    原文:iphone 输入/输出流异步读写数据 分类: iphone2012-05-30 14:50 2484人阅读 评论(1) 收藏 举报 iphoneattributesinterfacepaths ...

  7. 死磕salt系列-salt配置文件

    这篇文件主要用来解释一下salt配置中常用的参数,其他的参数可以参考官网文档. 基础参数 interface: 服务器监听地址. ipv6: 是否启用ipv6. max_open_files: 最大文 ...

  8. 【[APIO2010]巡逻】

    \(APIO\)的题就是非常难啊 首先看到\(k=1\)的情况,显然我们只需要找到一条直径把这条直径的两端连起来就好了 因为我们连这一条新边的实质是使得这一条链上的边不需要重复经过了,我们想让走的边尽 ...

  9. MyBatis(2)-全局配置文件

    本文的代码是在MyBatis(1)-简单入门基础之上进行学习的,如有不懂请先看此博文MyBatis(1)-简单入门! 1)配置文件的安装 --->在联网的情况下,点击去下载http://myba ...

  10. Java50道经典习题-程序22 递归求阶乘

    题目:利用递归方法求5!.分析:递归公式:n*factorial(n-1); public class Prog22 { public static void main(String[] args) ...