(1)Merge Intervals

https://leetcode.com/problems/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].

思路:贪心思想。首先根据intervals的start进行排序。排完序之后,先将第一个interval加入vector,接着判断之前加入vector的interval和后面一个interval比较。因为是排好序的,所以,只需要判断新的interval的start是否大于前面一个加入vector的interval的end。如果大于,则将后面的interval加入vector,如果不大于,则肯定有交集,两者合并。

struct Interval{
int start;
int end;
Interval():start(),end(){}
Interval(int s,int e):start(s),end(e){}
};
bool cmp(const Interval &left,const Interval & right){
if (left.start == right.start){
return left.end < right.end;
}else{
return left.start < right.start;
}
}
class Solution {
public:
/*struct cmp{
bool operator()(const Interval &left,const Interval & right){
if (left.start == right.start){
return left.end < right.end;
}else{
return left.start < right.start;
}
}
};*/ vector<Interval> merge(vector<Interval> &intervals) {
vector<Interval> result;
if (intervals.size() == || intervals.empty()){
return result;
}
sort(intervals.begin(),intervals.end(),cmp);
result.push_back(intervals[]);
int index = ;
for (int i = ; i < intervals.size(); i++){
if (intervals[i].start <= result[index].end){
int end = max(intervals[i].end,result[index].end);
result[index].end = end;
//index++;
//result.push_back(intervals[i]);
}else{
result.push_back(intervals[i]);
index++;
}
}
return result;
}
};

(2)Insert Interval

https://leetcode.com/problems/insert-interval/

Title:

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].

思路:我最直接的想法就是先找到newInterval的start在哪两个intervals之间,然后再找到end。虽然也能做,但是代码很复杂,不精练

class Solution {
public:
vector<Interval> insert(vector<Interval> & intervals,
Interval newInterval) {
vector<Interval> result;
if (intervals.size() == || intervals.empty()){
result.push_back(newInterval);
return result;
}
int i;
bool flag = false;
for (i = ; i < intervals.size();) {
if (intervals[i].start >= newInterval.start) {
int start, end;
if (i == ) {
start = newInterval.start;
} else {
if (intervals[i-].end >= newInterval.start){
start = intervals[i - ].start;
result.pop_back();
}else
start = newInterval.start; }
while (i < intervals.size() && intervals[i].start <= newInterval.end) {
i++;
}
if(i == intervals.size()){
end = max(intervals[i-].end,newInterval.end);
Interval interval(start,end);
result.push_back(interval);
}else{
end = max(intervals[i - ].end, newInterval.end);
Interval interval(start, end);
result.push_back(interval);
for (int j = i; j < intervals.size(); j++)
result.push_back(intervals[j]);
}
flag = true;
break;
} else {
result.push_back(intervals[i]);
i++;
}
}
if (!flag){
if (intervals[i-].end < newInterval.start){
//result.push_back(intervals[i-1
result.push_back(newInterval);
}else{
result.pop_back();
int start = intervals[i-].start;
int end = max(intervals[i-].end,newInterval.end);
Interval interval(start,end);
result.push_back(interval);
}
} return result;
} };

然后看了网上其他人的想法

Quickly summarize 3 cases. Whenever there is intersection, created a new interval.

遍历一遍vector,根据上述三种情况考虑添加。

vector<Interval> insert(vector<Interval> &intervals, Interval newInterval){
vector<Interval> result;
for (int i = ; i < intervals.size(); i++){
if (intervals[i].end < newInterval.start){
result.push_back(intervals[i]);
}else if (intervals[i].start > newInterval.end){
result.push_back(newInterval);
newInterval = intervals[i];
}else{
newInterval = Interval(min(intervals[i].start,newInterval.start),max(intervals[i].end,newInterval.end));
}
}
result.push_back(newInterval);
return result;
}

LeetCode: Interval的更多相关文章

  1. [LeetCode] Find Right Interval 找右区间

    Given a set of intervals, for each of the interval i, check if there exists an interval j whose star ...

  2. [LeetCode] Insert Interval 插入区间

    Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...

  3. Leetcode: Find Right Interval

    Given a set of intervals, for each of the interval i, check if there exists an interval j whose star ...

  4. Java for LeetCode 057 Insert Interval

    Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...

  5. [LeetCode]题解(python):057-Insert Interval

    题目来源 https://leetcode.com/problems/insert-interval/ Given a set of non-overlapping intervals, insert ...

  6. 【题解】【区间】【二分查找】【Leetcode】Insert Interval & Merge Intervals

    Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...

  7. leetcode Insert Interval 区间插入

    作者:jostree  转载请注明出处 http://www.cnblogs.com/jostree/p/4051169.html 题目链接:leetcode Insert Interval 使用模拟 ...

  8. [Leetcode] Binary search--436. Find Right Interval

      Given a set of intervals, for each of the interval i, check if there exists an interval j whose st ...

  9. 【一天一道LeetCode】#57. Insert Interval

    一天一道LeetCode系列 (一)题目 Given a set of non-overlapping intervals, insert a new interval into the interv ...

随机推荐

  1. [设计模式] 19 观察者模式 Observer Pattern

    在GOF的<设计模式:可复用面向对象软件的基础>一书中对观察者模式是这样说的:定义对象间的一种一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都得到通知并被自动更新.当一个 ...

  2. $POST数组论证($GET || $COOKIE || $REQUEST 同理)

    我觉得还是有多个$_POST  如果只有一个$_POST,那么,多个人[同时]提交的话就不好处理  或者一个$_POST 时间限制(如同时钟周期)处理(不可能,不然响应没这么快) 或者 一个$_POS ...

  3. JavaScript 函数参数是传值(byVal)还是传址(byRef)?

    对于“JavaScript 函数参数是传值(byVal)还是传址(byRef)”这个问题,普遍存在一个误区:number,string等“简单类型”是传值,Number, String, Object ...

  4. 安装 NoMachine(NX) client and server

    (1)Windows上直接安装nxclient-3.5.0-9.exe即可 (2)Linux上 准备linux rpms nxclient-3.5.0-7.x86_64.rpmnxnode-3.5.0 ...

  5. Unity3D与iOS的交互设计<ViewController 的跳转>

    原地址:http://www.aichengxu.com/article/%CF%B5%CD%B3%D3%C5%BB%AF/28797_12.html Unity3D与iOS的交互设计<View ...

  6. mysqlbinlog工具基于日志恢复详细解释

    如果每天都会生成大量的二进制日志,这些日志长时间不清理的话,将会对磁盘空间带来很大的浪费,所以定期清理日志是DBA维护mysql的一个重要工作 1)RESET MASTER在上面查看日志存放的文件夹中 ...

  7. time wait duo

    linux 下Time_wait过多问题解决 分类: linux FAQ2011-07-14 11:20 3485人阅读 评论(0) 收藏 举报 linux服务器tcp通讯活动ssh 问题起因: 自己 ...

  8. tornado的cookie和secure cookie

    tornado里面有关几个cookie的处理,在web.py文件里. get_cookie, set_cookie普通的设置cookie, clear_cookie, clear_all_cookie ...

  9. Bash 小知识点

    变量定义的时候=两边不能有空格,例如: a='Hello World' 如果变量和其它字符相连,可以用{}把变量引起来,这样就可以和相连的字符隔离 除了在变量赋值和在FOR循环语句头中,BASH中的变 ...

  10. 【转】 wget 命令用法详解

    wget是在Linux下开发的开放源代码的软件,作者是Hrvoje Niksic,后来被移植到包括Windows在内的各个平台上.它有以下功能和特点:(1)支持断点下传功能:这一点,也是网络蚂蚁和Fl ...