Leetcode 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].
题目的意思是将相交得区间合并,典型的贪心算法
首先将区间先按照start进行排序,
然后保存先前区间的start和end
如果当前的start > 先前的end,说明当前的区间与之前的区间不想交,则将先前的区间放入结果中,同时更新start和end
如果当前的start < 先前的end,说明当前的区间与先前的区间相交,故比较当前的end与先前的end,如果当前的end大于先前的end,则更新先前的end为当前的end
bool cmp(const Interval& a, const Interval& b){
if(a.start!=b.start) return a.start < b.start;
else return a.end < b.end;
}
class Solution {
public:
vector<Interval> merge(vector<Interval> &intervals) {
sort(intervals.begin(), intervals.end(), cmp);
vector<Interval> res;
if(intervals.size() == ) return res;
int start = intervals[].start, end = intervals[].end;
for(int i = ; i < intervals.size(); ++ i){
if(intervals[i].start > end){
res.push_back(Interval(start,end));
start = intervals[i].start, end = intervals[i].end;
}else{
end = max(end, intervals[i].end);
}
}
res.push_back(Interval(start,end));
return res;
}
};
Leetcode Merge Intervals的更多相关文章
- LeetCode: Merge Intervals 解题报告
Merge IntervalsGiven a collection of intervals, merge all overlapping intervals. For example,Given [ ...
- [LeetCode] Merge Intervals 排序sort
Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...
- [LeetCode] Merge Intervals 合并区间
Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6],[8, ...
- [leetcode]Merge Intervals @ Python
原题地址:https://oj.leetcode.com/problems/merge-intervals/ 题意: Given a collection of intervals, merge al ...
- LeetCode() Merge Intervals 还是有问题,留待,脑袋疼。
感觉有一点进步了,但是思路还是不够犀利. /** * Definition for an interval. * struct Interval { * int start; * int end; * ...
- 56[LeetCode] .Merge Intervals
Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums s ...
- 【题解】【区间】【二分查找】【Leetcode】Insert Interval & Merge Intervals
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- [Leetcode][Python]56: Merge Intervals
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 56: Merge Intervalshttps://oj.leetcode. ...
- Merge Intervals - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Merge Intervals - LeetCode 注意点 区间是无序的 每个区间start一定小于end 解法 解法一:首先以start的值从小到大来 ...
随机推荐
- Unity Development with VS Code
https://code.visualstudio.com/Docs/runtimes/unity
- Mac OSX定位命令路径的方法
可以使用which命令来定位一个命令. http://www.cyberciti.biz/faq/how-do-i-find-the-path-to-a-command-file/
- 【转~】初识贝塞尔曲线(Bézier curve)
本文图文大多转自http://www.html-js.com/article/1628 QAQ我居然去扒维基,,,看不懂啊,,,我要去补数学,,, 在做变形小鸡的时候用到CSS3 transition ...
- Idea 开发 web项目
1.经历 很久没有搞 web 项目了,最近一段时间搞过很多次了,但是总是在 mac 上部署失败. 2.方法: 用idea 新建一个模板的 Spring MVC 项目,部署就可以了. 3.参考: htt ...
- 如何 实现PHP多版本的 共存 和 切换?
编译PHP时指定路径 ./configure --prefix=/opt/php/php-5.6 make && make install 这样可以PHP版本就可以安装到一个特别的路径 ...
- 2015-9月份,Android开发,面试题总结,主要记录没有答出来的问题
9月13日,秒针面试(跪) 1.使用HTML5写Android 与本地应用比较 9月21日,百度一面(跪)1.Android的整个启动过程,什么阶段启动了什么进程,或者服务 2.Android系统框架 ...
- opencv二值化处理
#include "stdafx.h"//对一张图片进行二值化处理 IplImage *pSrclmg =NULL;//载入的图片IplImage *pDeclmg =NULL;/ ...
- alert 替代效果smoke.js
在一些表单等需要弹窗提醒的时候,每个浏览器都有一个alert(),comfirm()函数能弹出信息,但是浏览器的自带的这种效果样式不统一,而且都固定在页面顶部: smoke.js轻量级的JS插件,他标 ...
- BZOJ 3261: 最大异或和
Description 一个序列,支持两个操作. 1.在序列尾加入一个数. 2.询问 [l,r] 中与 x 异或值最大的数. \(n\leqslant 3*10^5\) Sol 可持久化 Trie 树 ...
- python之路十一
RabbitMQ基本概念RabbitMQ , 是一个使用 erlang 编写的 AMQP (高级消息队列协议) 的服务实现. 简单来说, 就是一个功能强大的消息队列服务.通常我们谈到队列服务, 会有三 ...