30. Insert Interval【LintCode by java】
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】的更多相关文章
- 156. Merge Intervals【LintCode by java】
Description Given a collection of intervals, merge all overlapping intervals. Example Given interval ...
- 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 ...
- 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 ...
- 158. Valid Anagram【LintCode by java】
Description Write a method anagram(s,t) to decide if two strings are anagrams or not. Clarification ...
- 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 ...
- 173. Insertion Sort List【LintCode by java】
Description Sort a linked list using insertion sort. Example Given 1->3->2->0->null, ret ...
- 172. Remove Element【LintCode by java】
Description Given an array and a value, remove all occurrences of that value in place and return the ...
- 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 ...
- 211. String Permutation【LintCode by java】
Description Given two strings, write a method to decide if one is a permutation of the other. Exampl ...
随机推荐
- Java实例---flappy-bird实例[最终版]
代码分析 解析版: Java实例---flappy-bird实例解析 完整版: TestBirdFly.java package testfly; import java.awt.Color; imp ...
- Linux telnet命令详解
telnet命令通常用来远程登录.telnet程序是基于TELNET协议的远程登录客户端程序.Telnet协议是TCP/IP协议族中的一员,是Internet远程登陆服务的标准协议和主要方式.它为用户 ...
- Mac appium iOS 安装命令
1. brew install node 2. npm install -g appium #或者用安装包安装 3. brew install libimobiledevice --HEAD # in ...
- JavaScript 变量声明提升
JavaScript 变量声明提升 一.变量提升的部分只是变量的声明,赋值语句和可执行的代码逻辑还保持在原地不动 二.在基本的语句(或者说代码块)中(比如:if语句.for语句.while语句.swi ...
- 021.12 IO流 ObjectStream对象序列化
内容:通过文件存储对象我们遇到的问题,需要保存对象到硬盘中,如何解决这个就是用来解决的 用法:1.创建流对象FileOutputstream2.创建ObjectOutputStream对象与FileO ...
- procedure of intall and setup centos 6.5
select webserver install option,and select ,uncheck the postgresql option selected some “… platform” ...
- vue开发知识点汇总
网址: https://www.tuicool.com/articles/Zb2Qre2;
- JavaScript的DOM_通过计算后样式来获取
虽然可以通过 style 来获取单一值的 CSS 样式,但对于复合值的样式信息,就需要通过计算样式来获取. DOM2 级样式,window 对象下提供了 getComputedStyle()方法.接受 ...
- BZOJ 1012 最大数maxnumber 线段树
题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=1012 题目大意: 见链接 思路: 直接用线段树模拟一下就可以了. #include&l ...
- python中执行shell命令
查看输出结果 import os output = os.popen('cat 6018_gap_5_predict/solusion2/solusion2_0-1.txt | wc -l') pri ...