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

/**
* 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. memset函数详解

    语言中memset函数详解(2011-11-16 21:11:02)转载▼标签: 杂谈 分类: 工具相关  功 能: 将s所指向的某一块内存中的每个字节的内容全部设置为ch指定的ASCII值, 块的大 ...

  2. c# 第一个实例 通哥

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  3. 不使用ASP.NET中的服务器控件将如何上传文件?

    遇到文件的上传时,可能会有大部分的开发者喜欢使用服务器控件,虽然很方便,但是却不能很好的控制,不具灵活性. 现给出例子,使用html标签语言灵活的控制文件的上传. 1.html部分 <input ...

  4. 关于sql 2005 版本问题

    win7可以安装:标准版  其他的都安装不了 win2003 2008 :可以安装任何版本

  5. 解决maven创建web项目卡死在generator插件(转)

    如下图所示:在Properties中添加一个参数archetypeCatalog=internal,不加这个参数,在maven生成骨架的时候将会非常慢,有时候会直接卡住. 理由 archetypeCa ...

  6. CSS3 Gradient

    CSS3CSS3发布很久了,现在在国外的一些页面上常能看到他的身影,这让我羡慕已久,只可惜在国内为了兼容IE,让这一项技术受到很大的限制,很多Web前端人员都望而止步.虽然如此但还是有很多朋友在钻研C ...

  7. height与line-height

    1.网页的所有元素可以分为块元素和行元素.一行文字所在的一个逻辑区域是行元素,其他的元素就都是块元素line-height只针对行元素,height针对其他所有元素 2. width,height对于 ...

  8. Lucky 2048 - The secret of being lucky

    Lucky 2048 uses a normal distribution to create "lucky" start. Generally speaking, it prov ...

  9. protobuf简介和使用

    1.Protocol Buffers简介 Protocol Buffers (ProtocolBuffer/ protobuf )是Google公司开发的一种数据描述语言,类似于XML能够将结构化数据 ...

  10. ENTITYFRAMEWORKCORE 二使用配置文件来配置数据库链接

    首先 配置文件现在已经变成appsettings.json, 先添加一个连接字符串 "ConnectionStrings": { "PWDatabase": & ...