60. Insert Interval && Merge Intervals
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].
思路: 因为区间按 start 升序,且无重叠。所以插入区间和每一个元素分三种情况考虑。在左边,在右边(此两种情况直接拿区间出来)或者交叉(则更新插入区间范围)。 利用变量 out 判断新的区间是否已经放入。
/**
* 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) {
vector<Interval> vec;
bool out = true;
for(size_t i = 0; i < intervals.size(); ++i) {
if(intervals[i].end < newInterval.start) {
vec.push_back(intervals[i]);
} else if(intervals[i].start > newInterval.end) {
if(out) { vec.push_back(newInterval); out = false;}
vec.push_back(intervals[i]);
} else {
newInterval.start = min(newInterval.start, intervals[i].start);
newInterval.end = max(newInterval.end, intervals[i].end);
}
}
if(out)
vec.push_back(newInterval);
return vec;
}
};
Merge Intervals
Given a collection of intervals, merge all overlapping intervals.
For example, Given [1,3],[2,6],[8,10],[15,18], return [1,6],[8,10],[15,18].
思路: 先按 start 排序。然后,判断当前区间和前一区间是否重叠。若没重叠,则放入;若重叠,则更新前一区间 end, 舍弃当前区间。
/**
* Definition for an interval.
* struct Interval {
* int start;
* int end;
* Interval() : start(0), end(0) {}
* Interval(int s, int e) : start(s), end(e) {}
* };
*/
bool cmp(Interval a, Interval b) {
return a.start < b.start;
}
class Solution {
public:
vector<Interval> merge(vector<Interval> &intervals) {
sort(intervals.begin(), intervals.end(), cmp);
vector<Interval> vec;
for(size_t i = 0; i < intervals.size(); ++i) {
if(vec.empty()) vec.push_back(intervals[i]);
else if(intervals[i].start <= vec.back().end)
vec.back().end = max(intervals[i].end, vec.back().end);
else vec.push_back(intervals[i]);
}
return vec;
}
};
60. Insert Interval && Merge Intervals的更多相关文章
- 【题解】【区间】【二分查找】【Leetcode】Insert Interval & Merge Intervals
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- Insert Interval & Merge Intervals
Insert Intervals Given a non-overlapping interval list which is sorted by start point. Insert a new ...
- 56. Merge Intervals 57. Insert Interval *HARD*
1. Merge Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[ ...
- 合并区间 · Merge Intervals & 插入区间 · Insert Interval
[抄题]: 给出若干闭合区间,合并所有重叠的部分. 给出的区间列表 => 合并后的区间列表: [ [ [1, 3], [1, 6], [2, 6], => [8, 10], [8, 10] ...
- [LeetCode] Merge Interval系列,题:Insert Interval,Merge Intervals
Interval的合并时比较常见的一类题目,网上的Amazon面经上也有面试这道题的记录.这里以LeetCode上的例题做练习. Merge Intervals Given a collection ...
- leetcode 56. Merge Intervals 、57. Insert Interval
56. Merge Intervals是一个无序的,需要将整体合并:57. Insert Interval是一个本身有序的且已经合并好的,需要将新的插入进这个已经合并好的然后合并成新的. 56. Me ...
- Leetcode: Merge/Insert Interval
题目 Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[ ...
- 间隔问题,合并间隔(merge interval),插入间隔(insert interval)
Merge Interval: Given a collection of intervals, merge all overlapping intervals. For example,Given ...
- leetcode 57 Insert Interval & leetcode 1046 Last Stone Weight & leetcode 1047 Remove All Adjacent Duplicates in String & leetcode 56 Merge Interval
lc57 Insert Interval 仔细分析题目,发现我们只需要处理那些与插入interval重叠的interval即可,换句话说,那些end早于插入start以及start晚于插入end的in ...
随机推荐
- Python的平凡之路(7)
一.面向对象高级语法部分 1.静态方法.类方法.属性方法 ...
- input , textarea 边框问题
一.去掉边框: 看看基本的HTML: 复制代码 代码如下: <div class="wrap"> <input type="text" cla ...
- iar 错误解决
使用原来备份的项目可以正确烧写并进入调试状态,但使用新项目则报错,错误提示为Failed to load debugee: E:\工作\项目-农业\KaCES-F\Debug\Exe\kaces.tx ...
- php-访问数据库
建一个连接,造一个连接对象 $db = new MySQLi("host","username","passwd","databa ...
- ios项目接入sdk事项
使用cocos2d-x引擎创建的项目在xcode里可以看到都带有一个ios目录,把要接入的sdk的包含.framework库文件和.bundle的资源文件的父目录拖入到xcode项目里的这个ios目录 ...
- 在Swift中应用Grand Central Dispatch(上)转载自的goldenfiredo001的博客
尽管Grand Central Dispatch(GCD)已经存在一段时间了,但并非每个人都知道怎么使用它.这是情有可原的,因为并发很棘手,而且GCD本身基于C的API在 Swift世界中很刺眼. 在 ...
- CSS3 动画基础
该文为译文转载,原文地址:http://tech.163.com/mobile/10/0106/09/5SB96QSM00112K88.html 译文:你需要知道的CSS3 动画技术 原文:Wha ...
- [原创]cocos2d-x研习录-第二阶 概念类之节点类(CCNode)
节点类CCNode在基本概念中并不存在,它是为了建立基本概念之间的关联关系而抽象出来的中间辅助类.这个类在Cocos2D-x中极为重要,它为概念类之间搭建了一座宏伟的桥梁.它的继承关系图如下: ...
- QTableWidget实用技巧(转)
http://blog.csdn.NET/mingxia_sui/article/details/7681863 在使用Qt不多的日子里,已经两次用到了QTableWidget这个控件,也慢慢的习惯和 ...
- Python字典实现三级菜单
################################################ # Task Name: 三级菜单 # # Description:打印省.市.县三级菜单 # # 可 ...