【leetcode】Insert Interval
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]
.
/**
* Definition for an interval.
* struct Interval {
* int start;
* int end;
* Interval() : start(0), end(0) {}
* Interval(int s, int e) : start(s), end(e) {}
* };
*/
class Solution {
public:
vector<Interval> insert(vector<Interval> &intervals, Interval newInterval) { int n=intervals.size();
vector<Interval> result; if(n==)
{
result.push_back(newInterval);
return result;
} int start=newInterval.start;
int end=newInterval.end; bool isfindStart=false;
bool isfindEnd=false;
bool addCur=true; for(int i=;i<n;i++)
{
addCur=true; if(!isfindStart)
{
//和当前区间没有交叉,且在start的前面
if(start>intervals[i].end)
{
//需要添加当前元素
addCur=true;
//当前元素为最后一个元素时,则认为start找到了
if(i==n-) isfindStart=true;
}
else
{
//和当前区间有交叉 或者start小于当前区间 start=min(start,intervals[i].start);
//后续就不用再找start了
isfindStart=true; //如果start和end没有和其他Interval有交叉
if(end<intervals[i].start) addCur=true;
else addCur=false;
}
} if(!isfindEnd)
{
//需要继续找end
if(end>intervals[i].end)
{ if(start<=intervals[i].end) addCur=false; if(i==n-)
{
isfindEnd=true; if(addCur) result.push_back(intervals[i]);
result.push_back(Interval(start,end)); continue;
}
}
else
{
//end小于当前区间,此时找到了end
end=end>=intervals[i].start?intervals[i].end:end; //找到了end
isfindEnd=true;
if(end>=intervals[i].start) addCur=false; result.push_back(Interval(start,end));
if(addCur) result.push_back(intervals[i]); continue;
}
} if(addCur) result.push_back(intervals[i]);
}
return result;
}
};
【leetcode】Insert Interval的更多相关文章
- 【题解】【区间】【二分查找】【Leetcode】Insert Interval & Merge Intervals
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- 【leetcode】Insert Interval(hard)★
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- 【Leetcode】【Hard】Insert Interval
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- 【LeetCode】986. Interval List Intersections 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 日期 题目地址:https://leetco ...
- 【leetcode】986. Interval List Intersections
题目如下: Given two lists of closed intervals, each list of intervals is pairwise disjoint and in sorted ...
- 【leetcode】986. Interval List Intersections (双指针)
You are given two lists of closed intervals, firstList and secondList, where firstList[i] = [starti, ...
- 【LeetCode】排序 sort(共20题)
链接:https://leetcode.com/tag/sort/ [56]Merge Intervals (2019年1月26日,谷歌tag复习) 合并区间 Input: [[1,3],[2,6], ...
- 【LeetCode】380. Insert Delete GetRandom O(1) 解题报告(Python)
[LeetCode]380. Insert Delete GetRandom O(1) 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxu ...
- 【LeetCode】436. Find Right Interval 解题报告(Python)
[LeetCode]436. Find Right Interval 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...
随机推荐
- thinkphp 3.2 视图模型 实例 视图查询结果 二维数组 合并
使用视图模型查询的时候 结果是这样的 array(6) { [0] => array(5) { ["picTitle"] => string(7) "标题2& ...
- Python之路【目录】 2
http://www.cnblogs.com/wupeiqi/articles/4938499.html
- vim does not map customized key?
it is a long time confusing me that why my customized key map in vim does not work? Some vim configu ...
- CF459B Pashmak and Flowers (水
Pashmak and Flowers Codeforces Round #261 (Div. 2) B. Pashmak and Flowers time limit per test 1 seco ...
- HTML5语义化标签
在HTML5中最基础也是比较好理解的也就是语义化标签了,,顾名思义语义化也就是可以直接读懂的标签~,这样我们在项目开发过程中不但自己不会因为5花8门的标签命名而伤脑筋,跟同事对接项目也会节约很多时间~ ...
- POJ 2486 Apple Tree
好抽象的树形DP......... Apple Tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6411 Accepte ...
- QT编写上位机程序一定要初始化变量以及谨慎操作指针
背景: 在编写QT上位机界面时,界面在运行的时候经常出现卡死或者直接挂掉的怪现象. 正文: 上位机有个函数为check_receive():该函数的作用为定时调用循环检测USB是否有数据.若有,则将信 ...
- SQL中关于日期的常用方法
mysql数据库: logintime >= STR_TO_DATE('$$START_TIME','%Y-%m-%d %H:%i:%s') AND logintime < STR_TO_ ...
- 【python网络编程】新浪爬虫:关键词搜索爬取微博数据
上学期参加了一个大数据比赛,需要抓取大量数据,于是我从新浪微博下手,本来准备使用新浪的API的,无奈新浪并没有开放关键字搜索的API,所以只能用爬虫来获取了.幸运的是,新浪提供了一个高级搜索功能,为我 ...
- css 设计总结
一.背景图片的拉伸: backgroud-size 说明: http://www.w3school.com.cn/cssref/pr_background-size.asp 效果: http:// ...