156. Merge Intervals【easy】
Given a collection of intervals, merge all overlapping intervals.
Given intervals => merged intervals:
[ [
[1, 3], [1, 6],
[2, 6], => [8, 10],
[8, 10], [15, 18]
[15, 18] ]
]
O(n log n) time and O(1) extra space.
解法一:
/**
* Definition of Interval:
* classs Interval {
* int start, end;
* Interval(int start, int end) {
* this->start = start;
* this->end = end;
* }
*/ class Solution {
public:
/**
* @param intervals: interval list.
* @return: A new interval list.
*/
static bool cmp(const Interval &a, const Interval &b) {
return (a.start < b.start);
} vector<Interval> merge(vector<Interval>& intervals) {
vector<Interval> ans;
if (intervals.empty()) {
return ans;
} sort(intervals.begin(), intervals.end(), cmp);
ans.push_back(intervals[]);
for (int i = ; i < intervals.size(); i++) {
if (ans.back().end >= intervals[i].start) {
ans.back().end = max(ans.back().end, intervals[i].end);
} else {
ans.push_back(intervals[i]);
}
}
return ans;
}
};
先排序,然后逐个判断是否需要合并
156. Merge Intervals【easy】的更多相关文章
- 156. Merge Intervals【LintCode by java】
Description Given a collection of intervals, merge all overlapping intervals. Example Given interval ...
- 21. Merge Two Sorted Lists【easy】
21. Merge Two Sorted Lists[easy] Merge two sorted linked lists and return it as a new list. The new ...
- 88. Merge Sorted Array【easy】
88. Merge Sorted Array[easy] Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 ...
- 170. Two Sum III - Data structure design【easy】
170. Two Sum III - Data structure design[easy] Design and implement a TwoSum class. It should suppor ...
- 160. Intersection of Two Linked Lists【easy】
160. Intersection of Two Linked Lists[easy] Write a program to find the node at which the intersecti ...
- 206. Reverse Linked List【easy】
206. Reverse Linked List[easy] Reverse a singly linked list. Hint: A linked list can be reversed eit ...
- 203. Remove Linked List Elements【easy】
203. Remove Linked List Elements[easy] Remove all elements from a linked list of integers that have ...
- 83. Remove Duplicates from Sorted List【easy】
83. Remove Duplicates from Sorted List[easy] Given a sorted linked list, delete all duplicates such ...
- 142. Linked List Cycle II【easy】
142. Linked List Cycle II[easy] Given a linked list, return the node where the cycle begins. If ther ...
随机推荐
- as3垃圾回收机制
as3垃圾回收机制 垃圾回收机制详解 能力越大责任越大,这对actionscript3.0来说一点没错.引入这些新控件带来一个副作用:垃圾收集器不再支持自动为你收集 垃圾等假设.也就是说Flash开发 ...
- rbac控制下无法创建poddisruptionbudgets
先通过下面命令找到具体的命名空间的rbac kubectl get role --all-namespaces kubectl get role aaa -o yaml 然后倒入到yaml文件中添加 ...
- Jenkins升级;yum 安装软件升级
1.使用yum安装的jenkins升级方法: 先升级yum yum update 后升级jenkins yum update jenkins 但是上面的方法,限制于yum的版本,不能更新到jenkin ...
- Jquery事件冒泡
事件冒泡 什么是事件冒泡 在一个对象上触发某类事件(比如单击onclick事件),如果此对象定义了此事件的处理程序,那么此事件就会调用这个处理程序,如果没有定义此事件处理程序或者事件返回true,那么 ...
- scrapy-splash抓取动态数据例子七
一.介绍 本例子用scrapy-splash抓取36氪网站给定关键字抓取咨询信息. 给定关键字:个性化:融合:电视 抓取信息内如下: 1.资讯标题 2.资讯链接 3.资讯时间 4.资讯来源 二.网站信 ...
- centos6.8服务器配置之编译安装PHP、配置nginx
php version 5.6.31.nginx version: nginx/1.10.2 1.下载: wget http://cn2.php.net/distributions/php-5.6.3 ...
- iOS 8 Share Extension Safari URL Example(在iOS中分享url的样例)
ios8 的Extension给我们提供了非常多奇妙的功能.以后分享内容再也不用进入app了,让我们的手机更安全,以下我们以在safari 浏览器中分享一个web url 来讲述Share Exten ...
- Android微信支付V3版
由于公司需求做微信APP支付,在集成过程中也遇到各种问题,比如说签名错误,body编码必须为UTF-8.APP端无法调用支付页面直接到支付结果页面.结果为null,code=-1等等: 1.签名错误问 ...
- Adb分析及获取root权限
Adb的全称为Android Debug Bridge,起到通过PC对Android系统的调试桥的作用,是一个多用途的工具,它能够执行多种命令,还能提供一个shell.这儿简单介绍一下Adb的代码结构 ...
- bitbucket git push 项目503失败
忙活了一天,要把项目push到远程仓库保存: 一直稳定的bitbucket今天突然push不成功了,出了503错误 谷歌了各种原因,还以为是连接出了问题 打开bitbucket State 页面之后, ...