LeetCode OJ:Insert Interval
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary).
You may assume that the intervals were initially sorted according to their start times.
Example 1:
Given intervals [1,3],[6,9], insert and merge [2,5] in as [1,5],[6,9].
Example 2:
Given [1,2],[3,5],[6,7],[8,10],[12,16], insert and merge [4,9] in as [1,2],[3,10],[12,16].
This is because the new interval [4,9] overlaps with [3,5],[6,7],[8,10].
类似以前的merge Intervals,只不过这里实际上是要将一个Interval插入到内部之后,然后再merge一下
而且这里的intervals在这里首先是已经排好序了的:
首先是一个带二分搜索的C++的方法:
class Solution {
public:
static bool comp(Interval a,Interval b){
return a.start<b.start;
}
vector<Interval> insert(vector<Interval>& intervals, Interval newInterval) {
if(intervals.size()==){
intervals.push_back(newInterval);
return intervals;
}
// sort(intervals.begin(),intervals.end(),comp);
if(intervals[].start>newInterval.end){
intervals.insert(intervals.begin(),newInterval);
return intervals;
}
else if(intervals[intervals.size()-].end<newInterval.start){
intervals.push_back(newInterval);
return intervals;
}
int left = binaryS(newInterval.start,intervals,,intervals.size()-, true);
int right = binaryS(newInterval.end, intervals,left, intervals.size()-,false);
int delLeft,delRight;
cout<<left<<right;
if(left == && intervals[].start>newInterval.start){
delLeft = left;
}
else if(intervals[left].end>= newInterval.start){
newInterval.start = intervals[left].start;
delLeft = left;
}
else{
delLeft = left+;
} if(right == intervals.size()- && intervals[right].end<newInterval.end){
delRight = right;
}
else if(intervals[right].start<= newInterval.end){
newInterval.end = intervals[right].end;
delRight = right;
}
else{
delRight = right-;
}
cout<<delLeft<<" "<<delRight<<endl;
vector<Interval> result; for(int i=;i<delLeft;i++){
result.push_back(intervals[i]);
}
result.push_back(newInterval);
for(int i=delRight+;i<intervals.size();i++){
result.push_back(intervals[i]);
}
return result;
// for(int i=delLeft;i<=delRight;i++){
// intervals.erase(intervals.begin()+delLeft);
// }
// intervals.push_back(newInterval);
// return intervals;
}
int binaryS(int x, vector<Interval> & intervals, int low, int high, bool isStart){
if(isStart){ while(low < high){
int mid = (low + high + ) /;
cout<<low<<high;
if(intervals[mid].start <= x){
low = mid;
}
else{
high = mid-;
}
}
}
else{
while(low < high){
int mid = (low + high) /;
// cout<<low<<high;
if(intervals[mid].end < x){
low = mid+;
}
else{
high = mid;
}
}
}
return low;
}
};
java版本的代码如下所示:
/**
* Definition for an interval.
* public class Interval {
* int start;
* int end;
* Interval() { start = 0; end = 0; }
* Interval(int s, int e) { start = s; end = e; }
* }
*/
public class Solution {
public List<Interval> insert(List<Interval> intervals, Interval newInterval) {
List<Interval> ret = new ArrayList<Interval>();
int sz = intervals.size();
if(sz == 0){
ret.add(newInterval);
return ret;
}
int prev = 0, next = 1;
while(next < sz){
if(newInterval != null && intervals.get(prev).end >= newInterval.start){
intervals.get(prev).end = Math.max(intervals.get(prev).end, newInterval.end);
newInterval = null;
}else if(newInterval == null){
if(intervals.get(prev).end >= intervals.get(next).start){
intervals.get(prev).end = Math.max(intervals.get(prev).end, intervals.get(next).end);
next++;
}else{
ret.add(intervals.get(prev));
prev = next;
next++;
}
}else{
ret.add(intervals.get(prev));
prev++;
next++;
}
}
intervals.get(prev).end = Math.max(intervals.get(prev).end, intervals.get(sz-1).end);
ret.add(intervals.get(prev));
return ret;
}
}
LeetCode OJ:Insert Interval的更多相关文章
- LeetCode OJ:Integer to Roman(转换整数到罗马字符)
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...
- [LeetCode] Merge Interval系列,题:Insert Interval,Merge Intervals
Interval的合并时比较常见的一类题目,网上的Amazon面经上也有面试这道题的记录.这里以LeetCode上的例题做练习. Merge Intervals Given a collection ...
- 【LeetCode】57. Insert Interval [Interval 系列]
LeetCode中,有很多关于一组interval的问题.大体可分为两类: 1.查看是否有区间重叠: 2.合并重叠区间; 3.插入新的区间: 4. 基于interval的其他问题 [ 做题通用的关键 ...
- 【LeetCode】57. Insert Interval
Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals ( ...
- LeetCode OJ:Serialize and Deserialize Binary Tree(对树序列化以及解序列化)
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
- LeetCode OJ:Search Insert Position(查找插入位置)
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- 【一天一道LeetCode】#57. Insert Interval
一天一道LeetCode系列 (一)题目 Given a set of non-overlapping intervals, insert a new interval into the interv ...
- [leetcode sort]57. Insert Interval
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- LeetCode OJ:Valid Number
Validate if a given string is numeric. Some examples:"0" => true" 0.1 " => ...
随机推荐
- shell下如何删除文件的某一列
答:cat file | awk '{$1=null;print $0}' (删除第一列)
- 【核心API开发】Spark入门教程[3]
本教程源于2016年3月出版书籍<Spark原理.机制及应用> ,在此以知识共享为初衷公开部分内容,如有兴趣,请支持正版书籍. Spark综合了前人分布式数据处理架构和语言的优缺点,使用简 ...
- 基于Java的三种对象持久化方式
1:序列化技术: 序列化的过程就是将对象写入字节流和从字节流中读取对象.将对象状态转换成字节流之后,可以用java.io包中的各种字节流类将其保存到文件中,可以通过管道或线程读取,或通过网络连接将对象 ...
- windows 命令行中使用winrar
加入环境变量即可 我的电脑右键属性->高级->环境变量->系统变量->编辑 变量名:path 变量值:C:/Program Files/WinRAR; --变量值为WinRAR ...
- [ios]IOS的AppDelegate方法中的事件触发调用 以及 关闭 ios应用程序
IOS的AppDelegate方法中的事件触发调用 参考:http://blog.sina.com.cn/s/blog_a573f7990101bphp.html //当应用程序将要进入非活动状态执行 ...
- WAV文件格式解析及处理
RIFF file format RIFF全称为资源互换文件格式(Resources Interchange File Format),是Windows下大部分多媒体文件遵循的一种文件结构.RIFF文 ...
- Java之美[从菜鸟到高手演变]系列之博文阅读导航
随着博文越来越多,为博客添加一个导航很有必要!本博客将相继开通Java.CloudFoundry.Linux.Ruby等专栏,都会设立目录,希望读者朋友们能更加方便的阅读! 在阅读的过程中有任何问题, ...
- 快速搭建一个简易的KMS 服务
xu言: 之前,闹的沸沸扬扬的KMS激活工具自身都存在问题的事.让我们对以前的什么小马激活.kms激活.各种激活工具都去打了一个深深的“?”,到底哪些能用.哪些不能用.有些还注明的里面必须要关闭杀毒软 ...
- week 1
day1 订正 学习AC自动机 day2 mobius反演 对偶图 codeforces day3 ZR模拟赛 订正 day4 复习AC自动机 题库 https://www.cnblogs.com/c ...
- Hololens 开发环境配置(转)
转自 Vangos Pterneas, 4 Apr 2016 CPOL 5.00 (1 vote) vote 1vote 2vote 3vote 4vote 5 The past few days h ...