Merge Intervals 运行比较快
class Solution {
public:
static bool cmp(Interval &a,Interval &b)
{
return a.start<b.start;
}
vector<Interval> merge(vector<Interval>& intervals) {
vector<Interval> res;
if(intervals.size()==)return res;
sort(intervals.begin(),intervals.end(),cmp);
int i=;
int first=;
int j=first;
Interval tmpin;
while(i<intervals.size()){
//第一种情况比如:[1,2] [3,4]
if(intervals[i].start>intervals[j].end){tmpin.start=intervals[first].start;tmpin.end=intervals[j].end;res.push_back(tmpin);
first=i;
j=first;
i=i+;
}
//第二种 比如: [2,6] [3,7]
else if(intervals[i].start<=intervals[j].end&&intervals[i].end>intervals[j].end)
{
i++;
j=i-;
}
//第三种 比如: [2,6] [1,3]
else
{
i++;
}
}
if(intervals[j].end>=intervals[i-].end) {tmpin.start=intervals[first].start;tmpin.end=intervals[j].end;res.push_back(tmpin);}
else{tmpin.start=intervals[first].start;tmpin.end=intervals[i-].end;res.push_back(tmpin);}
return res;
}
};
Merge Intervals 运行比较快的更多相关文章
- LeetCode: Merge Intervals 解题报告
Merge IntervalsGiven a collection of intervals, merge all overlapping intervals. For example,Given [ ...
- 【leetcode】Merge Intervals
Merge Intervals Given a collection of intervals, merge all overlapping intervals. For example,Given ...
- 【leetcode】Merge Intervals(hard)
Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...
- 60. Insert Interval && Merge Intervals
Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals ( ...
- 【题解】【区间】【二分查找】【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. ...
- [array] leetcode-56. Merge Intervals - Medium
leetcode-56. Merge Intervals - Medium descrition Given a collection of intervals, merge all overlapp ...
- 【leetcode】 Merge Intervals
Merge Intervals Given a collection of intervals, merge all overlapping intervals. For example,Given ...
- Insert Interval & Merge Intervals
Insert Intervals Given a non-overlapping interval list which is sorted by start point. Insert a new ...
随机推荐
- 使用 Wireshark 调试 HTTP/2 流量
https://imququ.com/post/http2-traffic-in-wireshark.html
- vs2008注册组件
开始—运行 输入 regsvr32+路径
- Spring异常抛出触发事务回滚
Spring.EJB的声明式事务默认情况下都是在抛出unchecked exception后才会触发事务的回滚 /** * 如果在spring事务配置中不为切入点(如这里的切入点可以定义成test*) ...
- "递归"实现"约瑟夫环","汉诺塔"
一:约瑟夫环问题是由古罗马的史学家约瑟夫提出的,问题描述为:编号为1,2,-.n的n个人按顺时针方向围坐在一张圆桌周围,每个人持有一个密码(正整数),一开始任选一个正整数作为报数上限值m,从第一个人开 ...
- Linux如何查看进程、杀死进程、启动进程等常用命令
Linux如何查看进程.杀死进程.启动进程等常用命令 关键字: linux 查进程.杀进程.起进程1.查进程 ps命令查找与进程相关的PID号: ps a 显示现行终端机下的所有程序,包括 ...
- HDOJ(1115)多边形重心
Lifting the Stone http://acm.hdu.edu.cn/showproblem.php?pid=1115 题目描述:输入n个顶点(整数),求它们围成的多边形的重心. 算法:以一 ...
- WPF打印、预览、导出PDF
本人很懒,已找到可使用样例 例: http://www.cnblogs.com/guogangj/archive/2013/02/27/2934733.html
- R随笔(2)
1,查看R中的变量模式(对象的数据类型)mode() 2,因子(factor), table()可以获取多个因子的交叉表,可以知道每个因子出现的次数 > g<-c("f&quo ...
- spark job, stage ,task介绍。
1. spark 如何执行程序? 首先看下spark 的部署图: 节点类型有: 1. master 节点: 常驻master进程,负责管理全部worker节点. 2. worker 节点: 常驻wor ...
- java环境基础步骤 svn
eclipse里安装SVN插件,一般来说,有两种方式: 直接下载SVN插件,将其解压到eclipse的对应目录里 使用eclipse 里Help菜单的"Install New Softwar ...