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]
.
对若干个区间进行合并,使合并后的区间没有重叠
先对区间按照左边界排序,然后顺序扫描,合并重叠的区间即可。 本文地址
代码1在原区间数组上操作,不使用额外的空间,但是需要删除多余的区间,这样会移动数组元素
/*** 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 {
private:static bool comp(Interval a, Interval b){return a.start < b.start;}
public:vector<Interval> merge(vector<Interval> &intervals) {if(intervals.empty())return intervals;sort(intervals.begin(), intervals.end(), comp);vector<Interval>::iterator it1 = intervals.begin(), it2 = it1 + 1;while(it1 != intervals.end() && it2 != intervals.end()){if(it2->start <= it1->end){if(it1->end < it2->end)it1->end = it2->end;it2++;}else{//[it1+1, it2)范围内的区间可以从原数组删除it1 = intervals.erase(it1+1, it2);it2 = it1 + 1;}}if(it1 != intervals.end())intervals.erase(it1 + 1, it2);return intervals;}
};
代码2用新数组来存储合并后的区间
/*** 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 {
private:static bool comp(Interval a, Interval b){return a.start < b.start;}
public:vector<Interval> merge(vector<Interval> &intervals) {if(intervals.empty())return intervals;sort(intervals.begin(), intervals.end(), comp);vector<Interval> res;res.push_back(intervals[0]);for(int i = 1; i < intervals.size(); i++){Interval &p = res.back();if(intervals[i].start > p.end)res.push_back(intervals[i]);else if(intervals[i].end > p.end)p.end = intervals[i].end;}return res;}
};
【版权声明】转载请注明出处:http://www.cnblogs.com/TenosDoIt/p/3714681.html
转载于:https://www.cnblogs.com/TenosDoIt/p/3714681.html
LeetCode:Merge Intervals相关推荐
- [LeetCode]Merge Intervals
题目:Merge Intervals 给定n个区间合并重合区间 思路: 先按照区间起点排序,然后合并下面情况: 1.起点相同,以最大的终点为新的终点: 2.前一个终点大于后一个的起点. /****** ...
- [leetcode]Merge Intervals @ Python
原题地址:https://oj.leetcode.com/problems/merge-intervals/ 题意: Given a collection of intervals, merge al ...
- LeetCode Merge Intervals
Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6],[8, ...
- LeetCode - Merge Intervals
02.08.2021 小白一枚,文章仅当日记本记录学习进度 ;) 如被浏览,请多多指教,非常感谢大家纠错与建议! (因留学顺便练习英语,所以部分用英文笔记,并无他意) Solution 1 - sor ...
- 【细节实现题】LeetCode 56. Merge Intervals
LeetCode 56. Merge Intervals Solution1:我的答案 这道题思路不难,有个坑爹的地方在于输入的区间向量未必是有序的,所以利用优先队列priority_queue来排序 ...
- 【leetcode】56. Merge Intervals 相邻线段归并
1. 题目 Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2, ...
- C#LeetCode刷题之#56-合并区间(Merge Intervals)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3676 访问. 给出一个区间的集合,请合并所有重叠的区间. 输入: ...
- 56. Merge Intervals(合并区间)解法(C++ 注释)
56. Merge Intervals(合并区间) 1. 题目描述 2. 排序(Sorting) 2.1 解题思路 2.2 实例代码 1. 题目描述 给出一个区间的集合,请合并所有重叠的区间. 示例 ...
- LeetCode 56. Merge Intervals
原题链接在这里:https://leetcode.com/problems/merge-intervals/ 题目: Given a collection of intervals, merge al ...
最新文章
- TPU3.0今日上岗!谷歌AI芯片甩竞争对手好几条街!
- Doxygen基本用法
- UA池和IP代理池使用
- Mysql disk write 高_优化系列|实例解析MySQL性能瓶颈排查定位
导读
排查过程
- 在ubuntu安装使用miniconda
- iText中给pdf内容添加水印
- 递归法:从n个小球中取m个小球(不放回),共有多少种取法?
- 前端后台的爱恨情仇——接口调试
- C# 设置开机自启动
- 佳能g2800清废墨_佳能G2800打印机清零
- win2008服务器系统玩红警,WIN10 64位系统完美运行红色警戒2教程
- html5的geolocation 定位误差大的解决办法
- 【12NOIP普及组】质因数分解
- This is a CONNECT tunnel, through which encrypted HTTPS traffic flows.
- 关于DSP系统时钟的一些理解
- 单片机学习笔记————51单片机实现两片联级74HC595驱动16个LED灯(把74HC595驱动程序翻译成类似单片机IO口直接驱动的方式)
- UOS20编译Qt程序:搭环境、解决bug
- ndk编译 错误:dlopen failed:cannot locate symbol 一个符号 referenced by 你的库.so/.a lang.UnsatisfiedLinkErr
- 我的世界java皮肤展开图,我的世界:如果将6种怪物皮肤展开,你能认出几个?图5难倒老MC...
- K8s 还是 k3s?This is a question