56. Merge Intervals (Array; Sort)
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].
注意点:
1。并不是像例子中给出的那样,前一个interval必定在后一个interval的前边。
所以我们先要对start进行排序。必须把start小的放在前面,然后按序递增,否则会出现这样的错误
/**
* Definition for an interval.
* struct Interval {
* int start;
* int end;
* Interval() : start(0), end(0) {}
* Interval(int s, int e) : start(s), end(e) {}
* };
*/
class Solution {
public:
static bool compare(Interval v1, Interval v2)
{
if(v1.start < v2.start)
return true;
else if(v1.start > v2.start)
return false;
else
return v1.end < v2.end;
} vector<Interval> merge(vector<Interval>& intervals) {
vector<Interval> result;
if(intervals.empty()) return result; sort(intervals.begin(),intervals.end(),compare);
result.push_back(intervals[]);
for(int i = ; i < intervals.size(); i++){
if(result[result.size()-].end >= intervals[i].end) //totally contain
continue; if(result[result.size()-].end >= intervals[i].start){//there's overlap
result[result.size()-].end = intervals[i].end;
}
else{ //there's no overlap
result.push_back(intervals[i]);
}
} return result;
}
};
56. Merge Intervals (Array; Sort)的更多相关文章
- leetcode 56. Merge Intervals 、57. Insert Interval
56. Merge Intervals是一个无序的,需要将整体合并:57. Insert Interval是一个本身有序的且已经合并好的,需要将新的插入进这个已经合并好的然后合并成新的. 56. Me ...
- 刷题56. Merge Intervals
一.题目说明 题目是56. Merge Intervals,给定一列区间的集合,归并重叠区域. 二.我的做法 这个题目不难,先对intervals排序,然后取下一个集合,如果cur[0]>res ...
- 56. Merge Intervals - LeetCode
Question 56. Merge Intervals Solution 题目大意: 一个坐标轴,给你n个范围,把重叠的范围合并,返回合并后的坐标对 思路: 先排序,再遍历判断下一个开始是否在上一个 ...
- [Leetcode][Python]56: Merge Intervals
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 56: Merge Intervalshttps://oj.leetcode. ...
- [LeetCode] Merge Intervals 排序sort
Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...
- 56. Merge Intervals (JAVA)
Given a collection of intervals, merge all overlapping intervals. Example 1: Input: [[1,3],[2,6],[8, ...
- 56. Merge Intervals
题目: Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6], ...
- [leetcode]56. Merge Intervals归并区间
Given a collection of intervals, merge all overlapping intervals. Example 1: Input: [[1,3],[2,6],[8, ...
- 56. Merge Intervals 57. Insert Interval *HARD*
1. Merge Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[ ...
随机推荐
- Find minimum continuous subsequence tags
Given targetList, a list of strings representing the desired tags, and availableTagList, a list of s ...
- [转]winform CEF
下载地址: http://opensource.spotify.com/cefbuilds/index.html 来自: NanUI 作者
- 1085 Perfect Sequence (25 分)
1085 Perfect Sequence (25 分) Given a sequence of positive integers and another positive integer p. T ...
- https单向认证服务端发送到客户端到底会不会加密?
最近向大神请教了CA认证单向认证和双向认证的问题,有一点不太明白,单向认证的时候,为什么服务端发送到客户端的东西也是加密的?
- oracle 用函数返回对象集合
1.先要声明全局type:并且,字段变量类型要为object,不能为record: (1)CREATE OR REPLACE TYPE "DDD_BY_DEPT_STATISTISC&quo ...
- Ps操作技巧(快捷键大全)
一.工具箱(多种工具共用一个快捷键的可同时按[Shift]加此快捷键选取) 矩形.椭圆选框工具 [M] 移动工具 [V] 套索.多边形套索.磁性套索 [L] 魔棒工具 [W] 裁剪工具 [C] 切片工 ...
- 第9课 const和volatile分析
1. const只读变量——(注意不是真正常量,只是告诉编译器不能出现在赋值号左边!) (1)const修饰的变量是只读的,本质还是变量 (2)const修饰的局部变量在栈上分配空间 (3)const ...
- java基础:关于java流与文件操作
1.描述:流是字节数据或字符数据序列.Java采用输入流对象和输出流对象来支持程序对数据的输入和输出.输入流对象提供了数据从源点流向程序的管道,程序可以从输入流对象读取数据:输出流对象提供了数据从程序 ...
- [python爬虫] Selenium定向爬取PubMed生物医学摘要信息
本文主要是自己的在线代码笔记.在生物医学本体Ontology构建过程中,我使用Selenium定向爬取生物医学PubMed数据库的内容. PubMed是一个免费的搜寻引擎,提供生物医学方 ...
- 23.OGNL与ValueStack(VS)-调用普通类的构造方法
转自:https://wenku.baidu.com/view/84fa86ae360cba1aa911da02.html 建立一个新的类:Student,在此省略代码. 然后在loginSuc.js ...