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].

Show Tags

Array Sort

 

  这题其实想好思路很好解决,对于框,如果下个框开始在 其中间,则连在一起,否则单独为一个,这需要按start 排序便可以了,因为类中写自定义比较函数比较麻烦,所以一次写了好几个。
  1. 按start 排序
  2. 初始化变量curstart,curend,记录当前窗的位置。
  3. 与下个窗比较,如果其start < curend,更新 curend。
  4. 否则加入ret,并跟新curstart,curend
  5. 遍历结束,加入最后的窗。
 #include <iostream>
#include <vector>
#include <algorithm>
using namespace std; /**
* Definition for an interval.
*/
struct Interval {
int start;
int end;
Interval() : start(), end() {}
Interval(int s, int e) : start(s), end(e) {}
}; class Solution {
public:
vector<Interval> merge(vector<Interval> &intervals) {
sort(intervals.begin(),intervals.end(),\
[] (Interval i1,Interval i2)\
{return i1.start<i2.start; }); /// sort(intervals.begin(),intervals.end(),help_fun); /** struct my_cmp{
bool operator ()(Interval i1,Interval i2){
return i1.start<i2.start;
}
}my_cmp1;
sort(intervals.begin(),intervals.end(),my_cmp());
sort(intervals.begin(),intervals.end(),my_cmp1);
*/ // for(int i=0;i<intervals.size();i++){
// cout<<intervals[i].start<<" "<<intervals[i].end<<endl;
// }
vector<Interval> ret;
if(intervals.size()<) return ret;
int curStart=intervals[].start,curEnd=intervals[].end;
for(int i=;i<intervals.size();i++){
if(curEnd>=intervals[i].start){
if(intervals[i].end>curEnd) curEnd=intervals[i].end;
continue;
}
ret.push_back(Interval(curStart,curEnd));
curStart = intervals[i].start;
curEnd = intervals[i].end;
}
ret.push_back(Interval(curStart,curEnd));
return ret;
} static bool help_fun(Interval i1,Interval i2)
{
return i1.start<i2.start;
}
}; int main()
{
vector<Interval> intervals={Interval(,),\
Interval(,),\
Interval(,),\
Interval(,)};
Solution sol;
vector<Interval> ret = sol.merge(intervals); for(int i=;i<ret.size();i++){
cout<<ret[i].start<<" "<<ret[i].end<<endl;
}
return ;
}

[LeetCode] Merge Intervals 排序sort的更多相关文章

  1. [LeetCode] Merge Intervals 合并区间

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

  2. LeetCode: Merge Intervals 解题报告

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

  3. [leetcode]Merge Intervals @ Python

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

  4. Leetcode Merge Intervals

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

  5. 56. Merge Intervals (Array; Sort)

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

  6. LeetCode() Merge Intervals 还是有问题,留待,脑袋疼。

    感觉有一点进步了,但是思路还是不够犀利. /** * Definition for an interval. * struct Interval { * int start; * int end; * ...

  7. 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 ...

  8. 【LeetCode】Merge Intervals 题解 利用Comparator进行排序

    题目链接Merge Intervals /** * Definition for an interval. * public class Interval { * int start; * int e ...

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

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

随机推荐

  1. Linux问题分析或解决_samba无法连接

    1. windows设置方面问题 问题:window能连接部分服务器的samba共享,一部分无法连接.报错如截图. 解决:前提---其他人连接都没有问题,发现有问题的连接服务器的电脑是win10,而w ...

  2. POJ 2763 Housewife Wind 树链拋分

    一.前言 这破题WA了一天,最后重构还是WA,最后通过POJ讨论版得到的数据显示,我看上去是把某个变量写错了..于是,还是低级错误背锅啊....代码能力有待进一步提升2333333 二.题意 某家庭主 ...

  3. Storm: 遇到问题总结

    1.没有ack : kafkaspout id 重复导致每次读最新没有数据. 2.由于storm提供的读取kafka的enternal工具存在bug,导致重复读取数据,致使数据不准确.storm bu ...

  4. mysql-show processlist之writing to net

    mysql提示Writing to net解决 最近发现某一个数据库cpu占用比较过.超过200%了. 首先查看数据库慢日志,设定慢日志5秒,基本上没有产生日,没有超过5秒的语句. show proc ...

  5. go经典练习题涉及流程控制-字符串-struct-map的数据类型的处理

    one:求1到100之间的质数 package main import ( "fmt" ) func isPrime(n int) bool { var flag = true f ...

  6. dcpromo(server2012不支持)

    dcpromo 编辑 dcpromo命令是一个“开关”命令.如果Windows 2000 Server计算机是成员服务器,则 运行dcpromo命令会安装活动目录,将其升级为域控制器:如果Window ...

  7. Azure继续降价云 价格战就此终结?

    [TechTarget中国原创] 刚刚跨入2016年,就听到了云降价这样一个消息,但是我们却不要期望降价之风如去年一样呼呼不绝. 微软公司在本周宣称,他们将在下个月对其D系列虚拟机实施高达17%的降价 ...

  8. leetcode 【 Find Peak Element 】python 实现

    题目: A peak element is an element that is greater than its neighbors. Given an input array where num[ ...

  9. 图说不为人知的IT传奇故事-1-计算机新生

    此系列文章为“图说不为人知的IT传奇故事”,各位大忙人可以在一分钟甚至几秒内了解把握整个内容,真可谓“大忙人的福利”呀!!希望各位IT界的朋友在钻研技术的同时,也能在文学.历史上有所把握.了解这些故事 ...

  10. IOS开发学习笔记035-UIScrollView-自动滚动

    让图片自动滚动的话,需要使使用定时器,循环计算当前页的页码.并且在拖动图片时停止计时器,停止拖动时启动计时器. 定时器 方法1: performSelector [self performSelect ...