merge-intervals 合并区间
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].
合并有重叠的区间,且原区间序列无序
/**
* 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(const Interval &first,const Interval &second)
{
if(first.start==second.start)
return first.end<second.end;
else
return first.start<second.start;
}
vector<Interval> merge(vector<Interval> &intervals) {
vector<Interval> ret;
int n=intervals.size();
int pre=, cur=;
sort(intervals.begin(),intervals.end(),compare);
while(cur<n)
{
while(cur<n&&intervals[pre].end>=intervals[cur].start)
{
intervals[pre].start=min(intervals[pre].start,intervals[cur].start);
intervals[pre].end=max(intervals[pre].end,intervals[cur].end);
cur++;
}
ret.push_back(intervals[pre]);
pre=cur; }
return ret;
}
};
merge-intervals 合并区间的更多相关文章
- [LeetCode] Merge Intervals 合并区间
Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6],[8, ...
- [LeetCode] 56 - Merge Intervals 合并区间
Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...
- 056 Merge Intervals 合并区间
给出一个区间的集合, 请合并所有重叠的区间.示例:给出 [1,3],[2,6],[8,10],[15,18],返回 [1,6],[8,10],[15,18].详见:https://leetcode.c ...
- Leetcode56. Merge Intervals合并区间
给出一个区间的集合,请合并所有重叠的区间. 示例 1: 输入: [[1,3],[2,6],[8,10],[15,18]] 输出: [[1,6],[8,10],[15,18]] 解释: 区间 [1,3] ...
- LeetCode 56. Merge Intervals 合并区间 (C++/Java)
题目: Given a collection of intervals, merge all overlapping intervals. Example 1: Input: [[1,3],[2,6] ...
- 【LeetCode每天一题】Merge Intervals(合并区间)
Given a collection of intervals, merge all overlapping intervals. Example 1: Input: [[1,3],[2,6],[8, ...
- 合并区间 · Merge Intervals & 插入区间 · Insert Interval
[抄题]: 给出若干闭合区间,合并所有重叠的部分. 给出的区间列表 => 合并后的区间列表: [ [ [1, 3], [1, 6], [2, 6], => [8, 10], [8, 10] ...
- [leetcode]56. Merge Intervals归并区间
Given a collection of intervals, merge all overlapping intervals. Example 1: Input: [[1,3],[2,6],[8, ...
- merge intervals(合并间隔)
Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...
- LeetCode 56. Merge Intervals (合并区间)
Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...
随机推荐
- Javascrip获取页面URL信息
使用Javascript可以方便获得页面的参数信息,常用的几种如下: 设置或获取对象指定的文件名或路径 window.location.pathname 设置或获取整个 URL 为字符串 wind ...
- 使用Logstash创建ES映射模版并进行数据默认的动态映射规则
本文配置为 ELK 即(Elasticsearch.Logstash.Kibana)5.5.1. Elasticsearch 能够自动检测字段的类型并进行映射,例如引号内的字段映射为 String,不 ...
- 洛谷 P1123 取数游戏
题目描述 一个N×M的由非负整数构成的数字矩阵,你需要在其中取出若干个数字,使得取出的任意两个数字不相邻(若一个数字在另外一个数字相邻8个格子中的一个即认为这两个数字相邻),求取出数字和最大是多少. ...
- 样条之EHMT插值函数
核心代码: ////////////////////////////////////////////////////////////////////// // 埃特金插值 ////////////// ...
- 使用rsync实现客户端与服务器端的文件同步与传送,重点是原理和参数解释
1.什么是rsync?-rsync是类unix系统下的数据镜像备份工具——remote sync.一款快速增量备份工具 Remote Sync,远程同步 支持本地复制,或者与其他SSH.rsync主机 ...
- 在ASP.NET MVC中以post方式传递数组参数的示例【转】
最近在工作中用到了在ASP.NET MVC中以post方式传递数组参数的情况,记录下来,以供参考. 一.准备参数对象 在本例中,我会传递两个数组参数:一个字符串数组,一个自定义对象数组.这个自定义对象 ...
- [leetcode]Path Sum @ Python
原题地址:https://oj.leetcode.com/problems/path-sum/ 题意: Given a binary tree and a sum, determine if the ...
- Android -- getWidth()与getMeasuredWidth()
getWidth() Return the width of the your view. Returns The width of your view, in pixels. 源代码: public ...
- CSS命名规范和规则
一.命名规则 ).尽量不缩写,除非一看就明白的单词 二.class的命名 (1).red { color: red; } .f60 {color: #f60; } .ff8600{ color: #f ...
- 机器学习之深入理解SVM
在浏览本篇博客之前,最好先查看一下我写的还有一篇文章机器学习之初识SVM(点击可查阅哦).这样能够更好地为了结以下内容做铺垫! 支持向量机学习方法包括构建由简至繁的模型:线性可分支持向量机.线性支持向 ...