LeetCode 56. 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].
题目标签:Array
这道题目给了我们一个区间的list,让我们返回一个list,是合并了所有有重叠的区间之后的list。这道题目的关键在于如何判断两个区间有重叠,根据原题给的例子可以看出,在按照每个区间的start排序之后很容易判断出是否相邻的两个区间有交集。我们看第一个区间[1,3] 中的end 3 > 第二个区间[2,6] 的start 2。在排序完之后,只要前面一个区间的end 大于等于 后面一个区间的start,它们就是重叠的。那么为了merge 这两个区间,保留第一个区间的start, 在两个end里面拿大的。设一个temp 等于第一个区间,遍历剩下的区间。然后每次拿temp 和这个区间比较,要注意的是,如果遇到了重叠的,把temp 更新为merge 后的区间,这时不需要加入list,因为还有可能继续和下一个区间重叠。当temp和这个区间不重叠的时候,把temp 加入list,再把这个区间设为新的temp。
Java Solution:
Runtime beats 89.25%
完成日期:07/20/2017
关键词:Array
关键点:利用comparator sort list,当A的end 大于等于 B的start,说明重叠
/**
* Definition for an interval.
* public class Interval {
* int start;
* int end;
* Interval() { start = 0; end = 0; }
* Interval(int s, int e) { start = s; end = e; }
* }
*/
public class Solution
{
public List<Interval> merge(List<Interval> intervals)
{
List<Interval> res = new ArrayList<>();
if(intervals.size() == 0)
return res; // sort the intervals list according to start
Collections.sort(intervals, new Comparator<Interval>(){
public int compare(Interval a, Interval b)
{
return a.start - b.start;
}
});; // get first one
Interval temp = intervals.get(0); // if intervals only has one element
if(intervals.size() == 1)
{
res.add(temp);
return res;
} // iterate intervals from [1] to end
for(int i=1; i<intervals.size(); i++)
{
// case 1: if temp interval end >= this interval start ->
// merge tempStart, max(tempEnd, thisEnd) and make this merge one as new temp;
if(temp.end >= intervals.get(i).start)
{
temp.end = Math.max(temp.end, intervals.get(i).end);
}
// case 2: if temp interval is not overlapping this interval ->
// add temp into list and make this new temp
else
{
res.add(temp);
temp = intervals.get(i);
}
}
// add the last temp into list
res.add(temp); return res;
}
}
参考资料:N/A
LeetCode 算法题目列表 - LeetCode Algorithms Questions List
LeetCode 56. Merge Intervals (合并区间)的更多相关文章
- [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 合并区间 (C++/Java)
题目: Given a collection of intervals, merge all overlapping intervals. Example 1: Input: [[1,3],[2,6] ...
- [leetcode]56. Merge Intervals归并区间
Given a collection of intervals, merge all overlapping intervals. Example 1: Input: [[1,3],[2,6],[8, ...
- leetcode 56. Merge Intervals 、57. Insert Interval
56. Merge Intervals是一个无序的,需要将整体合并:57. Insert Interval是一个本身有序的且已经合并好的,需要将新的插入进这个已经合并好的然后合并成新的. 56. Me ...
- [LeetCode] Merge Intervals 合并区间
Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6],[8, ...
- 【LeetCode每天一题】Merge Intervals(合并区间)
Given a collection of intervals, merge all overlapping intervals. Example 1: Input: [[1,3],[2,6],[8, ...
- 056 Merge Intervals 合并区间
给出一个区间的集合, 请合并所有重叠的区间.示例:给出 [1,3],[2,6],[8,10],[15,18],返回 [1,6],[8,10],[15,18].详见:https://leetcode.c ...
- LeetCode: 56. Merge Intervals(Medium)
1. 原题链接 https://leetcode.com/problems/merge-intervals/description/ 2. 题目要求 给定一个Interval对象集合,然后对重叠的区域 ...
- Leetcode56. Merge Intervals合并区间
给出一个区间的集合,请合并所有重叠的区间. 示例 1: 输入: [[1,3],[2,6],[8,10],[15,18]] 输出: [[1,6],[8,10],[15,18]] 解释: 区间 [1,3] ...
随机推荐
- Java:实现对象的比较 comparable接口和comparator接口
在实际应用中,我们往往有需要比较两个自定义对象大小的地方.而这些自定义对象的比较,就不像简单的整型数据那么简单,它们往往包含有许多的属性,我们一般都是根据这些属性对自定义对象进行比较的.所以Java中 ...
- Java多线程高并发学习笔记(三)——深入理解线程池
线程池最核心的一个类:ThreadPoolExecutor. 看一下该类的构造器: public ThreadPoolExecutor(int paramInt1, int paramInt2, lo ...
- 深度学习(一)cross-entropy softmax overfitting regularization dropout
一.Cross-entropy 我们理想情况是让神经网络学习更快 假设单模型: 只有一个输入,一个神经元,一个输出 简单模型: 输入为1时, 输出为0 神经网络的学习行为和人脑差的很多, 开始学习 ...
- Project Euler 92:Square digit chains C++
A number chain is created by continuously adding the square of the digits in a number to form a new ...
- JSP知识点大纲图
这是我整理出来的JSP知识点大纲图,具体的内容都可以在我的博文中找到-.
- GCD之死锁体会
1.先看下几句代码 1 2 3 4 5 6 7 dispatch_queue_t serialqueue=dispatch_queue_create("serialqueue", ...
- oracle sql*plus常用命令
一.sys用户和system用户Oracle安装会自动的生成sys用户和system用户(1).sys用户是超级用户,具有最高权限,具有sysdba角色,有create database的权限,该用户 ...
- 如何实现跨 Docker 主机存储?- 每天5分钟玩转 Docker 容器技术(73)
从业务数据的角度看,容器可以分为两类:无状态(stateless)容器和有状态(stateful)容器. 无状态是指容器在运行过程中不需要保存数据,每次访问的结果不依赖上一次访问,比如提供静态页面的 ...
- 三、js的函数
三.函数 函数是定义一次但却可以调用或执行任意多次的一段JS代码.函数有时会有参数,即函数被调用时指定了值的局部变量.函数常常使用这些参数来计算一个返回值,这个值也成为函数调用表达式的值. 1.函数声 ...
- 一次生产环境下MongoDB备份还原数据
最近开发一个版本的功能当中用到了MongoDB分页,懒于造数据,于是就研究了下从生产环境上导出数据到本地来进行测试. 研究了一下,发现MongoDB的备份还原和MySQL语法还挺类似,下面请看详细介绍 ...