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 ...
随机推荐
- POI创建Excel使用的常见的属性
public static void main(String[] args) { //创建新的Excel 工作簿 HSSFWorkbook workbook =new HSSFWorkbook(); ...
- [转]Insert, Update, and Delete Destination table with SSIS
本文转自:http://www.rad.pasfu.com/index.php?/archives/150-Insert,-Update,-and-Delete-Destination-table-w ...
- scikit-learn入门学习记录
一加载示例数据集 from sklearn import datasets iris = datasets.load_iris() digits = datasets.load_digits() 数据 ...
- HDU oj password
#include<stdio.h> #include<string.h> main() { int m; scanf("%d",&m); ch ...
- subline 配置,本地项目代码下断点来调试远程项目
参考: https://my.oschina.net/ptk/blog/299464 1. 文件 tts.sublime-project 的配置如下: { "folders": [ ...
- Java实现二维码技术探讨。
Java生成二维码方法有三种: 1: 使用SwetakeQRCode在Java项目中生成二维码 http://swetake.com/qr/ 下载地址 或着http://sourceforge.j ...
- Linux 倒引号、单引号、双引号
1.倒引号表示命令,用于命令替换,获取命令的返回结果. echo now is `date` 或者 echo now is $(date) 2.单引号 name=Andy 没有问题, 如果想 nam ...
- Android常用传感器用法一览(1)
1.传感器入门自从苹果公司在2007年发布第一代iPhone以来,以前看似和手机挨不着边的传感器也逐渐成为手机硬件的重要组成部分.如果读者使用过iPhone.HTC Dream.HTC Magic.H ...
- php抽象与接口的区别[转载]
来自:http://www.cnblogs.com/k5054/archive/2012/12/26/2834205.html 对于面向对象开发,抽象类与接口这两个东西是比较难理解的! 今天看了一整天 ...
- Gstreamer学习
Gstreamer学习笔记----Gstreamer架构设计思想 http://blog.csdn.net/tx3344/article/details/7497434 Gstreamer到底是个啥? ...