[LeetCode] 56. Merge Intervals(vector sort)

/**
* 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 cmp(Interval &a,Interval &b)
{
return a.start < b.start;
}
vector<Interval> merge(vector<Interval>& intervals)
{
if(intervals.empty()) return vector<Interval> {};
sort(intervals.begin(),intervals.end(),cmp);
vector<Interval> res;
res.push_back(intervals[]);
for(int i = ; i < intervals.size(); i ++)
{
if(intervals[i].start > res.back().end)
res.push_back(intervals[i]);
else
res.back().end = max(res.back().end, intervals[i].end);
}
return res;
}
};
[LeetCode] 56. Merge Intervals(vector sort)的更多相关文章
- LeetCode 56. Merge Intervals (合并区间)
Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...
- LeetCode: 56. Merge Intervals(Medium)
1. 原题链接 https://leetcode.com/problems/merge-intervals/description/ 2. 题目要求 给定一个Interval对象集合,然后对重叠的区域 ...
- leetcode 56. Merge Intervals 、57. Insert Interval
56. Merge Intervals是一个无序的,需要将整体合并:57. Insert Interval是一个本身有序的且已经合并好的,需要将新的插入进这个已经合并好的然后合并成新的. 56. Me ...
- 【leetcode】Merge Intervals(hard)
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 ...
- 34.Merge Intervals(合并序列)
Level: Medium 题目描述: Given a collection of intervals, merge all overlapping intervals. Example 1: I ...
- LeetCode OJ:Merge Intervals(合并区间)
Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8, ...
- Leetcode#56 Merge Intervals
原题地址 排序+合并,没啥好说的 第一次尝试C++的lambda表达式,有种写js的感觉,很神奇 c11就支持了lambda表达式,仔细想想,我学C++大概就是在09~10年,c11还没有发布,不得不 ...
- [LeetCode] 56. Merge Intervals 解题思路
Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...
随机推荐
- (转)Maven POM中的各种scope的行为总结
原地址:https://blog.csdn.net/cnweike/article/details/52221410 compile:默认的scope.任何定义在compile scope下的依赖将会 ...
- WebSocket协议 8 问
WebSocket是一种比较新的协议,它是伴随着html5规范而生的,虽然还比较年轻,但大多主流浏览器都已经支持.它使用方面.应用广泛,已经渗透到前后端开发的各种场景中. 对http一问一答中二式流程 ...
- Mysqldump自定义导出n条记录
很多时候DBA需要导出部分记录至开发.测试环境,因数据量需求较小,如果原库的记录多,且表数量也多,在用mysqldump命令导出时可以添加一个where参数,自定义导出n条记录,而不必全量导出. 示例 ...
- linux下SVN CVS命令大全
1.将文件checkout到本地目录 svn checkout path(path是服务器上的目录) 例如:svn checkout svn: // 192.168. 1.1 / pro / doma ...
- 清华大学《C++语言程序设计基础》线上课程笔记02---类与对象
类与对象 public是类的对外访问接口: 类内初始值 在定义类时对数据成员写初始值,在创建对象的时候,会使用类内初始值初始化数据成员: class Clock { public: void show ...
- Java+Selenium3自动化测试框架设计系列--href="javascript:void(0)"如何获得元素定位
经过前面两篇文章的铺 垫,我们这篇介绍,webdriver如何处理,一个浏览器上多个窗口之间切换的问题.我们先脑补这样一个测试场景,你在页面A点击一个连接,会在新的 tab窗口打开页面B,这个时候,你 ...
- nyoj 525 一道水题【字符串(分割)】
参考:https://blog.csdn.net/dxx_111/article/details/48154687 #include <iostream> #include <cst ...
- DAG上dp思想
DAG上DP的思想 在下最近刷了几道DAG图上dp的题目.要提到的第一道是NOIP原题<最优贸易>.这是一个缩点后带点权的DAG上dp,它同时规定了起点和终点.第二道是洛谷上的NOI导刊题 ...
- PADS9.5打开Altium designer09的原理图
1. 打开PADS Logic原理图工具,文件---导入 2. 选择Protel DXP这个选项,找到相应的文件即可打开.
- .NET中调用不安全代码
.NET中是不允许不安全的代码的,比如指针等.但有些特殊场合还是需要用到指针,这时候就需要在你的代码块上加上unsafe标签.如: 1: unsafe static void Main( ...