(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. 【HDOJ】【2089】不要62

    数位DP cxlove基础数位DP第一题 用容斥把所有的不吉利数字去掉就得到吉利数字的数量= =(满足区间减法) //HDOJ 2089 #include<cmath> #include& ...

  2. 并行编译加快 VS C++ 项目的编译速度

    最近编译的项目都比较大,话说自己的电脑配置还行,但编译所花的时间还是很长,遇到需要重新编译整个项目的时候真的有回宿舍睡一觉的冲动.昨天一不小心被我发现了一款软件Xoreax IncrediBuild ...

  3. float2int

    flaot转int时,会直接舍弃小数为,但是当把f所在的地址的数据当成int解析时,就是另外的情况了. #include<iostream> using namespace std; in ...

  4. hdu 4901

    一个简单的dp,比赛的时候太坚信自己的小聪明没用二维数组一直WA到死: #include<cstdio> #include<cstring> #define maxn 1009 ...

  5. oracle sql 性能 优化

    目录[-] (1) 选择最有效率的表名顺序(只在基于规则的优化器中有效):ORACLE的解析器按照从右到左的顺序处理FROM子句中的表名,FROM子句中写在最后的表(基础表 driving table ...

  6. 关于nginx限制IP或IP段的问题2011

    关于nginx限制IP或IP段的问题2011-04-08 16:46:39 分类: LINUX 最近有同事问需要在nginx中针对一些IP和IP段限制访问,通过了解以下方法可以解决问题:   首先建立 ...

  7. 【C++基础】指针好难啊,一点点啃——基本概念

    指针保存的是另一个对象的地址(概念真的很重要!!) ; int *ptr = &a;//*定义一个指向int类型的指针ptr, &a取变量a的地址 引用是对象的别名,多用于函数形参,引 ...

  8. **json_encode:让Json更懂中文(JSON_UNESCAPED_UNICODE)

    我们知道, 用PHP的json_encode来处理中文的时候, 中文都会被编码, 变成不可读的, 类似”\u***”的格式, 还会在一定程度上增加传输的数据量. 代码如下: <?php echo ...

  9. POJ 1054 The Troublesome Frog(枚举+剪枝)

    题目链接 题意 :给你r*c的一块稻田,每个点都种有水稻,青蛙们晚上会从水稻地里穿过并踩倒,确保青蛙的每次跳跃的长度相同,且路线是直线,给出n个青蛙的脚印点问存在大于等于3的最大青蛙走的连续的脚印个数 ...

  10. Java中LinkedList的remove方法真的耗时O(1)吗?

    这个问题其实来源于Leetcode的一道题目,也就是上一篇日志 LRU Cache.在使用LinkedList超时后,换成ArrayList居然AC了,而问题居然是在于List.remove(Obje ...