LeetCode - Merge Interval.
2014.2.26 21:28
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]
.
Solution:
The problem didn't guarantee that the set of intervals is sorted, so a sort() has to be performed first.
After sorting the set, the intervals are arranged first in the order of their x-coordinates, and y-coordinates later.
When scanning the intervals, there will be three possibilities:
1. the next interval is contained by the current one, ignore the next one and move on
2. the next interval overlaps with the current one, merge them and move on
3. the next interval is separated from the current one, move the "current" iterator to the next one
Total time complexity is O(n * log(n)). Space complexity is O(1).
Accepted code:
// 1CE, 1RE, 1AC, O(n) solution.
#include <cstdlib>
#include <algorithm>
#include <vector>
using namespace std;
/*
struct Interval {
int start;
int end;
Interval() : start(0), end(0) {}
Interval(int s, int e) : start(s), end(e) {}
};
*/
bool comparator(const Interval &a, const Interval &b)
{
if (a.start == b.start) {
return a.end < b.end;
} else {
return a.start < b.start;
}
} class Solution {
public:
vector<Interval> merge(vector<Interval> &intervals) {
int i;
int n = (int)intervals.size();
int tmp; for (i = ; i < n; ++i) {
if (intervals[i].start > intervals[i].end) {
tmp = intervals[i].start;
intervals[i].start = intervals[i].end;
intervals[i].end = tmp;
}
} for (i = ; i < n; ++i) {
if (!comparator(intervals[i - ], intervals[i])) {
// the array is not sorted;
break;
}
}
if (i < n) {
sort(intervals.begin(), intervals.end(), comparator);
} vector<Interval> result;
int next_i;
i = ;
while (i < n) {
next_i = i + ;
while (next_i < n) {
if (intervals[next_i].end <= intervals[i].end) {
// the next interval is included, thus skipped
++next_i;
} else if (intervals[next_i].start <= intervals[i].end) {
// the next interval is overlapped, thus merged
intervals[i].end = intervals[next_i].end;
++next_i;
} else {
// the next interval is non-overlapping, jump to next
break;
}
}
result.push_back(intervals[i]);
i = next_i;
} return result;
}
};
LeetCode - Merge Interval.的更多相关文章
- [LeetCode] Merge Interval系列,题:Insert Interval,Merge Intervals
Interval的合并时比较常见的一类题目,网上的Amazon面经上也有面试这道题的记录.这里以LeetCode上的例题做练习. Merge Intervals Given a collection ...
- Leetcode: Merge/Insert Interval
题目 Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[ ...
- leetcode 57 Insert Interval & leetcode 1046 Last Stone Weight & leetcode 1047 Remove All Adjacent Duplicates in String & leetcode 56 Merge Interval
lc57 Insert Interval 仔细分析题目,发现我们只需要处理那些与插入interval重叠的interval即可,换句话说,那些end早于插入start以及start晚于插入end的in ...
- 56. Merge Interval
56. Merge Interval 0. 参考文献 序号 文献 1 花花酱 LeetCode 56. Merge Intervals 2 [LeetCode] Merge Intervals 合并区 ...
- leetcode Insert Interval 区间插入
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4051169.html 题目链接:leetcode Insert Interval 使用模拟 ...
- LintCode 156: Merge Interval
LintCode 156: Merge Interval 题目描述 给出若干闭合区间,合并所有重叠的部分. 样例 给出的区间列表 => 合并后的区间列表: [ [ [1, 3], [1, 6], ...
- 间隔问题,合并间隔(merge interval),插入间隔(insert interval)
Merge Interval: Given a collection of intervals, merge all overlapping intervals. For example,Given ...
- Merge Interval leetcode java
题目: Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6] ...
- [LeetCode] Merge Sorted Array 混合插入有序数组
Given two sorted integer arrays A and B, merge B into A as one sorted array. Note:You may assume tha ...
随机推荐
- TP5.1:数据库的增删改查操作(基于数据库操作)
1.在app/index/controller文件夹下创建一个文件,名为:Operation 注意:起名一定要避开关键字,例如:mysql,curd等等,如果使用关键字起名,会造成报错! 在Opera ...
- Linux 使用第三方邮箱发邮件的设置
mail命令在Ubuntu下是需要安装的,使用下条命令进行安装: sudo apt-get install heirloom-mailx 在CentOS 下安装则是: yum install mail ...
- MSMQ学习笔记二——创建Message Queue队列
一.创建Message Queue队列的主要流程 1.定义MQQUEUEPROPS 结构: 2.设置消息队列属性: 3.初始化MQQUEUEPROPS 结构: 4.调用MQCreateQueue创建队 ...
- C++ POD类型
POD( Plain Old Data)概念: Arithmetic types (3.9.1), enumeration types, pointer types, and pointer to m ...
- Gym 100169A 最短路
题意:不久后滑铁卢将会变得非常冷,但是幸运的是,很多建筑都被桥梁和隧道连接着,所以你不需要总是走在外面.但是现在建筑 物之间的连接是错综复杂的,很难知道某两个建筑物之间的最优路线,所以需要你写程序判断 ...
- HDU(3560)成环,并查集
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3560 并查集查有几个块,修改了之前我的一个方法(用什么map),直接判断根节点的id是i的个数. 然后 ...
- eclipse的一些快捷键
ctrl + 1快速修复 ctrl + d 快速删除 ctrl + F11快速运行 ctrl + m 放大工作区 atl + /注释 ...
- Android Support v4,v7,v13的区别和应用场景
android-support-v4 是谷歌推出的兼容包,最低兼容Android1.6的系统,里面有类似ViewPager等控件.ViewPager在Android 1.6以下的版本是不自带的,所以要 ...
- ios视图层次结构
原文:http://blog.csdn.net/xingboss3/article/details/7890238 UIView表示屏幕上的一块矩形区域,它在App中占有绝对重要的地位,因为IOS中几 ...
- 剑指offer40
class Solution { public: void FindNumsAppearOnce(vector<int> data,int* num1,int *num2) { ) ret ...