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的值从小到大来 ...
随机推荐
- flume从kafka中读取数据
a1.sources = r1 a1.sinks = k1 a1.channels = c1 #使用内置kafka source a1.sources.r1.type = org.apache.flu ...
- c/c++程序员必须要掌握开源项目
作为一个经验丰富的C/C++程序员, 肯定亲手写过各种功能的代码, 比如封装过数据库访问的类, 封装过网络通信的类,封装过日志操作的类, 封装过文件访问的类, 封装过UI界面库等, 也在实际的项目中应 ...
- IOS开发:监听来电状态的改变。
#import <CoreTelephony/CTCallCenter.h> #import <CoreTelephony/CTCall.h> @property(nonato ...
- (翻译)Emacs Hooks
Table of Contents 1. 51.2.2 Hooks 51.2.2 Hooks Hooks(钩子或挂钩,为了保持文章的纯正性,这种专有名词不做翻译,后续以hooks为主),是定制化Ema ...
- Three.js入门
一.前段时候花了些功夫研究了下WebGL,了解了基本实体的实现原理和实现方法,现在回忆就只记得如果要我画个圆形,怀疑都要了我的命(那得画多少个三角形...).功夫不负有心人,今天学习Three.js得 ...
- python的各种编辑器-PyScripter、pycharm 、atom、vscode、Sublime Text等等
RT,本文主要列举python的各种编辑器-PyScripter.pycharm .atom.vscode.Sublime Text等等. PyScripter 开源 免费 windows only ...
- jQuery原生框架-----------------事件
jQuery.extend({ // 绑定事件 addEvent: function( ele, type, fn ) { // ele不是DOM,type不是字符串,fn不是函数,打包打走 if( ...
- JS在火狐浏览器下如何关闭标签?
首先,要确定火狐设置是否允许通过JS代码window.close()方法关闭标签. 确定方式如下: 在Firefox地址栏里输入 about:config 在配置列表中找到dom.allow_scri ...
- http数据返回值
HTTP 400 - 请求无效HTTP 401.1 - 未授权:登录失败HTTP 401.2 - 未授权:服务器配置问题导致登录失败HTTP 401.3 - ACL 禁止访问资源HTTP 401.4 ...
- 从UWP到SWIFT-页面间反向传值
页面1跳转到页面2,在页面2点击button后,页面1的内容被改变.实际使用 protocol(就是c#中的interface),将页面1的viewcontroller转换为protocol传入页面2 ...