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 ...
随机推荐
- seajs实例
点击文本改变: <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...
- [转]解决Eclipse更新ADT插件时遇到的Eclipse reports rendering library more recent than ADT plug-in问题
使用 SDK Manager 工具更新下载新版本后,无法显示可视化布局,同时提示 This version of the rendering library is more recent than y ...
- [Android Memory] Android系统中查看某个应用当前流量的方法
转载自: http://blog.sina.com.cn/s/blog_628cc2b70101dbyy.html 一.查看原理:某个应用的网络流量数据保存在系统的/proc/uid_stat/$UI ...
- Eclipse 构建Maven项目--普通web项目
一.Maven项目的新建 1.鼠标右键---->New----->Maven Project 2.直接点下一步 3.选中 maven-archetype-webapp 后点击下一步 4. ...
- Linux内核源码情景分析-系统调用
一.系统调用初始化 void __init trap_init(void) { ...... set_system_gate(SYSCALL_VECTOR,&system_call);//0x ...
- 查询mysql数据库中所有表名
查找所有表的语句 select table_name from information_schema.tables where table_schema='当前数据库';
- 登录Tomcat控制台
在http://localhost:8080界面中,右上角有三个按纽对应着三个控制台:Server Status控制台,Manager App控制台和Host Manager控制台.Server St ...
- ShopNC B2B2C多用户商城2014商业版,带微商城
据说价值7000RMB,咱们好站长资源网友分享出来的,非常感谢分享这么好的源码.此套ShopNC B2B2C多用户商城源码是2014版的,带有微商城,源码我们安装测试基本没发现啥问题,这两天将会完全免 ...
- liunx系统安装jdk的方法
1.下载jdk 下载地址: http://www.oracle.com/technetwork/java/javasebusiness/downloads/java-archive-downloads ...
- LoadRunner录制:检查点
LoadRunner怎么request是否执行成功呢?它通过判断服务器返回的HTTP状态码,如果是200 OK,那么VuGen就认为脚本运行通过. 但是很多时候事务执行失败并不一定返回错误的状态码,比 ...