LeetCode() Merge Intervals 还是有问题,留待,脑袋疼。
感觉有一点进步了,但是思路还是不够犀利。
/**
* 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 还是有问题,留待,脑袋疼。的更多相关文章
- LeetCode: Merge Intervals 解题报告
Merge IntervalsGiven a collection of intervals, merge all overlapping intervals. For example,Given [ ...
- [LeetCode] Merge Intervals 排序sort
Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...
- [LeetCode] Merge Intervals 合并区间
Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6],[8, ...
- [leetcode]Merge Intervals @ Python
原题地址:https://oj.leetcode.com/problems/merge-intervals/ 题意: Given a collection of intervals, merge al ...
- Leetcode Merge Intervals
Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...
- 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 ...
- 【题解】【区间】【二分查找】【Leetcode】Insert Interval & Merge Intervals
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- [Leetcode][Python]56: Merge Intervals
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 56: Merge Intervalshttps://oj.leetcode. ...
- Merge Intervals - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Merge Intervals - LeetCode 注意点 区间是无序的 每个区间start一定小于end 解法 解法一:首先以start的值从小到大来 ...
随机推荐
- readelf与动态库
使用arm-linux-gcc编译的可执行文件可能会无法在开发板上执行,并提示:-/bin/sh xxx not found 解决办法: 在主机上使用readelf -d xxx 来查看该程序所需要的 ...
- 自定义 Material Design风格的提示框
关闭 自定义 Material Design风格的提示框 2016-04-24 10:55 152人阅读 评论(0) 收藏 举报 版权声明:本文为博主原创文章,未经博主允许不得转载. 其实在14年谷歌 ...
- position窗口居中
position的四个属性值: relative absolute fixed static 下面分别讲述这四个属性. <div id="parent"> &l ...
- a primary example for Functional programming in javascript
background In pursuit of a real-world application, let’s say we need an e-commerce web applicationfo ...
- css position static | absolute | fixed | relative
<div id="bigbox1"> <div id="box1" class="box">box1</ ...
- [解决方案] pythonchallenge level 4
http://www.pythonchallenge.com/pc/def/linkedlist.php 查看页面源代码或者点击图片 http://www.pythonchallenge.com/pc ...
- Linux文件系统层次结构标准
该标准的目的是定义Linux文件系统的标准路径,使得开发者和用户可以在合理的位置找到需要的东西. Linux的文件布局的大体想法是将文件和目录分为如下3组: 对运行Linux的某一特定系统唯一的文件和 ...
- BeautifulSoup-find,findAll
BeautifulSoup的主要函数使用 from bs4 import BeautifulSouphtml = """<html><head>& ...
- BackTrack5-r3安装用户组-软件中心
所需文件包地址:http://pan.baidu.com/s/1i3ouc9v(64位更新包) 进入BT系统图形模式-将(用户组-软件中心)文件夹改名(a)并拖进BT系统图形桌面-打开BT终端输入:c ...
- 黑马-----内存模型和volatile详解
黑马程序员:Java培训.Android培训.iOS培训..Net培训 JAVA线程-内存模型和volatile详解 一.单核内存模型 1.程序运行时,将临时数据存放到Cache中 2.将CPU计算所 ...