【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 ...
随机推荐
- Python基础之【第三篇】
dir(): 默认打印当前模块的所有属性,如果传一个对象参数则打印当前对象的变量名 vars() 默认打印当前模块的所有属性,如果传一个对象参数则打印当前对象的变量名和值 reload() 将以前导入 ...
- Python开发【第十五篇】:Web框架之Tornado
概述 Tornado 是 FriendFeed 使用的可扩展的非阻塞式 web 服务器及其相关工具的开源版本.这个 Web 框架看起来有些像web.py 或者 Google 的 webapp,不过为了 ...
- linux下设置进程优先级方法!
Linux系统下提升进程优先级的办法 Linux系统进程的优先级取值:-20 到 19,数越大优先级越低. 可以通过top命令来查看,NI那一列. 改变进程的优先级的方法有两种: www ...
- FZU2169 shadow题解
http://acm.fzu.edu.cn/problem.php?pid=2169 Problem Description YL 是shadow国的国王,shadow国有N个城市.为了节省开支,sh ...
- Oracle CASE WHEN 用法介绍
1. CASE WHEN 表达式有两种形式 --简单Case函数 CASE sex WHEN '1' THEN '男' WHEN '2' THEN '女' ELSE '其他' END --Case搜索 ...
- log4net--不可多得的开源日志记录组件
log4net--不可多得的开源日志记录组件 1 前奏 一直在用log4net日志工具,却没时间写个日志给大家分享一下这个工具,趁最近比较空些,好好分享一下这个工具. 2 说明 Log4net介绍就不 ...
- 一次关于使用status作为变量引发的bug及思考
这个bug出现在一年前,当时自己大学还没毕业,刚刚进入一家公司实习.那个时候还没有用seajs或者requirejs那样的模块化管理的库,也没有用一个自执行的函数将要执行的代码包裹起来,于是bug就在 ...
- 【转载】Ubuntu 系列安装 Docker
系统要求 Docker 支持以下版本的Ubuntu操作系统: Ubuntu Xenial 16.04 (LTS) Ubuntu Wily 15.10 Ubuntu Trusty 14.04 (LTS) ...
- 解决python3 UnicodeEncodeError: 'gbk' codec can't encode character '\xXX' in position XX
从网上抓了一些字节流,想打印出来结果发生了一下错误: UnicodeEncodeError: 'gbk' codec can't encode character '\xbb' in position ...
- 深入理解计算机系统-从书中看到了异或交换ab两个值的新感
还得从一个很经典的面试题说起:不通过第三个变量来交换两个变量a,b的值... 一个很经典的答案是通过异或来解决: 第壹步:a=a^b; 第贰步:b=a^b; 第叁步:a=a^b; 以前提起" ...