感觉有一点进步了,但是思路还是不够犀利。

/**
* 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> merge(vector<Interval>& intervals) {
vector<pair<int,int>> r;
for(int i=0;i<intervals.size();i++)
r.push_back(make_pair(intervals[i].start,intervals[i].end));
sort(r.begin(),r.end());
int index=0;
vector<Interval> res;
while(index<r.size()){
int start=r[index].first;
int end=r[index].second;
if(start == INT_MAX)
{
index++;
continue;
}
for(int i=index+1;i<r.size();i++)
{
if(r[i].first == end){
end=r[i].second;
r[i].first=INT_MAX,r[i].second=INT_MAX; } }
while(r[index+1].first >= start && r[index+1].second<=end && r[index+1].second>=start&&r[index+1].second<=end)
{
index++;
}
while(end>=r[index+1].first && end<=r[index+1].second && index+1 <=r.size())
{
index++;
end=r[index].second;
}
res.push_back(Interval(start,end));
index++;
}
return res;
}
};

  本地测试正确,不知道哪里有问题

#include"stdafx.h"
#include<iostream>
#include<vector>
#include<set>
#include<math.h>
#include<map>
#include<sstream>
#include<algorithm>
#include<stack>
#include<queue>
using namespace std;
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> merge(vector<Interval>& intervals) {
auto next = intervals.begin();
auto pre = next++;
while (next != intervals.end()) {
if (pre->end >= next->start) {
pre->end = next->end;
next = intervals.erase(next);
}
else {
pre++;
next++;
}
}
return intervals;
}
}; int main()
{
vector<Interval> coll;
Interval a = Interval(1, 3);
Interval b = Interval(2, 6);
Interval c = Interval(8, 10);
Interval d = Interval(15, 18);
coll.push_back(a);
coll.push_back(b);
coll.push_back(c);
coll.push_back(d);
// coll.erase(coll.begin());
Solution s;
s.merge(coll);
for (auto i : coll)
cout << i.start << " " << i.end << endl;
system("pause");
return 0;
}

  

LeetCode() Merge Intervals 还是有问题,留待,脑袋疼。的更多相关文章

  1. LeetCode: Merge Intervals 解题报告

    Merge IntervalsGiven a collection of intervals, merge all overlapping intervals. For example,Given [ ...

  2. [LeetCode] Merge Intervals 排序sort

    Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...

  3. [LeetCode] Merge Intervals 合并区间

    Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6],[8, ...

  4. [leetcode]Merge Intervals @ Python

    原题地址:https://oj.leetcode.com/problems/merge-intervals/ 题意: Given a collection of intervals, merge al ...

  5. Leetcode Merge Intervals

    Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...

  6. 56[LeetCode] .Merge Intervals

    Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums s ...

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

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

  8. [Leetcode][Python]56: Merge Intervals

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 56: Merge Intervalshttps://oj.leetcode. ...

  9. Merge Intervals - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Merge Intervals - LeetCode 注意点 区间是无序的 每个区间start一定小于end 解法 解法一:首先以start的值从小到大来 ...

随机推荐

  1. CSS兼容问题实用建议

    CSS兼容问题,是美工最头痛的问题.做测试时,用谷哥和360浏览器(最新)都没有什么问题,用 IE6/IE8测试,问题就冒出来了.微软现在出IE10,我电脑上已经无法用IE6准确测试,IE-TESTE ...

  2. 重叠I/O模型

    一. 重叠I/O的概念当调用ReadFile和WriteFile时,如果最后一个参数lpOverlapped设置为NULL,那么线程就阻塞在这里,直到读写完指定的数据后,它们才返回.这样在读写大文件的 ...

  3. 使用WebDriverWait类处理等待(sleep)的问题

    用selenium进行web UI的自动化开发时,经常遇到loading需要等待的时候,或者需要验证一个action之后某个dialog是否呈现或者消失.对于这类情况是不建议用sleep(xx)来死等 ...

  4. CenOS 6.5下安装docker(转)

    2014-12-15 10:23 blessed24 To be Done的博客 字号:T | T 最近在自己的centos上搭建了一个Docker,顺便将一些常用操作记录下. AD:51CTO网+ ...

  5. Android Activity中获取当前焦点的控件,自动化输入EditText

    获取焦点的view对象 View view=getWindow().getDecorView().findFocus(); 如果是EditText if(view instanceof EditTex ...

  6. MySQL 配置文件中忘配置default-character-set引发的乱码问题

    今天,一开发同事使用jdbc连接数据库执行一条语句无结果集,但是通过sqlyou执行相同的语句有返回结果. 执行的语句where条件中含有中文,这应该是字符集引起的 此开发测试实例刚迁移不久的,查看迁 ...

  7. CI关于自动加载

    /application/config/autoload.php文件中定义自动加载的包.类库.helper.用户配置文件.语言文件.模块 类库会到/application/libraries目录或/s ...

  8. 新篇章,Golang 和 beego 初识

    beego 是一个快速开发 Go 应用的 HTTP 框架,他可以用来快速开发 API.Web 及后端服务等各种应用,是一个 RESTful 的框架,主要设计灵感来源于 tornado.sinatra ...

  9. 关于项目中owl文件中的类定义和属性定义

    <owl:Class rdf:about="www.isinonet.com/insider#XXX"> <rdfs:label>name</rdfs ...

  10. 背景建模post_processing常用opencv函数(怒了)

    1.saturate_cast<uchar>来说,就是把数据转换成8bit的0~255区间,负值变成0,大于255的变成255.如果是浮点型的数据,变成round最近的整数 2.cv::M ...