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].
链接: http://leetcode.com/problems/merge-intervals/
题解:
使用Comparator来对集合进行sort。 Comparator和Comparable是两个很重要的interface,再加上interable,runnable等,要好好掌握。
/**
* 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 == null || intervals.size() == 0)
return res;
Collections.sort(intervals, new IntervalComparator()); Interval last = intervals.get(0); for(int i = 1; i < intervals.size(); i++) {
Interval curr = intervals.get(i);
if(last.end < curr.start) {
res.add(last);
last = curr;
} else {
if(last.end < curr.end)
last.end = curr.end;
}
} res.add(last);
return res;
} public class IntervalComparator implements Comparator<Interval> {
@Override
public int compare(Interval a, Interval b) {
return a.start - b.start;
}
}
}
二刷:
Java:
依然是使用comparator来对整个interval数组进行排序,我们可以用anonymous comparator,或者是Java 8的lambda表达式。 下次把start重命名为prev可能更好一些。
使用Lambda:
Time Complexity - O(nlogn), Space Complexity - O(1)
/**
* 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 == null || intervals.size() == 0) {
return intervals;
}
Collections.sort(intervals, (Interval i1, Interval i2) -> (i1.start != i2.start ? i1.start - i2.start : i1.end - i2.end));
Interval start = intervals.get(0);
for (int i = 1; i < intervals.size(); i++) {
Interval tmp = intervals.get(i);
if (start.end < tmp.start) {
res.add(start);
start = tmp;
} else if (start.end < tmp.end){
start.end = tmp.end;
}
}
res.add(start);
return res;
}
}
使用Anonymous comparator:
Time Complexity - O(nlogn), Space Complexity - O(1)
/**
* 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 == null || intervals.size() == 0) {
return intervals;
}
Collections.sort(intervals, new Comparator<Interval>() {
public int compare(Interval i1, Interval i2) {
return i1.start != i2.start ? i1.start - i2.start : i1.end - i2.end;
}
});
Interval start = intervals.get(0);
for (int i = 1; i < intervals.size(); i++) {
Interval tmp = intervals.get(i);
if (start.end < tmp.start) {
res.add(start);
start = tmp;
} else if (start.end < tmp.end){
start.end = tmp.end;
}
}
res.add(start);
return res;
}
}
Updated:
class Solution {
public int[][] merge(int[][] intervals) {
if (intervals == null || intervals.length == 0) return new int[][] {};
Arrays.sort(intervals, (int[] i1, int[] i2) -> i1[0] != i2[0] ? i1[0] - i2[0] : i1[1] - i2[1]);
List<int[]> res = new ArrayList<>();
int[] lastInterval = intervals[0];
for (int i = 1; i < intervals.length; i++) {
if (intervals[i][0] <= lastInterval[1]) {
lastInterval[1] = Math.max(lastInterval[1], intervals[i][1]);
} else {
res.add(new int[]{lastInterval[0], lastInterval[1]});
lastInterval = intervals[i];
}
}
res.add(new int[]{lastInterval[0], lastInterval[1]});
return res.toArray(new int[res.size()][2]);
}
}
Reference:
https://leetcode.com/discuss/13953/a-simple-java-solution
https://leetcode.com/discuss/33434/a-clean-java-solution
https://leetcode.com/discuss/42344/7-lines-easy-python
https://leetcode.com/discuss/43383/easy-c-solution-with-explanations-o-nlogn-time-and-o-n-space
56. Merge Intervals的更多相关文章
- [Leetcode][Python]56: Merge Intervals
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 56: Merge Intervalshttps://oj.leetcode. ...
- leetcode 56. Merge Intervals 、57. Insert Interval
56. Merge Intervals是一个无序的,需要将整体合并:57. Insert Interval是一个本身有序的且已经合并好的,需要将新的插入进这个已经合并好的然后合并成新的. 56. Me ...
- 刷题56. Merge Intervals
一.题目说明 题目是56. Merge Intervals,给定一列区间的集合,归并重叠区域. 二.我的做法 这个题目不难,先对intervals排序,然后取下一个集合,如果cur[0]>res ...
- 56. Merge Intervals - LeetCode
Question 56. Merge Intervals Solution 题目大意: 一个坐标轴,给你n个范围,把重叠的范围合并,返回合并后的坐标对 思路: 先排序,再遍历判断下一个开始是否在上一个 ...
- 56. Merge Intervals 57. Insert Interval *HARD*
1. Merge Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[ ...
- 【LeetCode】56. Merge Intervals
Merge Intervals Given a collection of intervals, merge all overlapping intervals. For example,Given ...
- 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 ...
- LeetCode 56. Merge Intervals (合并区间)
Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...
随机推荐
- Html.TextBoxFor三元判断
@Html.TextBoxFor(item => item.DiscountOW,(Model.TripType == "单程" || (Model.TripType == ...
- jquery-easyui中datagrid扩展,隐藏显示表头功能
今天,后台中需要新增一个功能,用户可以自由选择显示的列,之后保存到本地localStroage中.所以扩展了easyui中datagrid的onHeaderContextMenu方法. 使用方法: _ ...
- 使用RX方式模拟DoubanFm的登陆
WP7下的Get Post都是异步的 关于RX http://www.cnblogs.com/yangecnu/archive/2012/11/03/Introducting_ReactiveExte ...
- DB 从zl.xml中导入数据库用户名及密码等!
package com.dy.java; import java.io.FileInputStream; import java.io.FileNotFoundException; import ja ...
- Hive表分区
必须在表定义时创建partition a.单分区建表语句:create table day_table (id int, content string) partitioned by (dt stri ...
- QQ群里收集的外企iOS开发的笔试题
一组外企iOS开发的笔试题,您能回答出来吗?从群里收集来的. 1 why can't NSArray contain NSInteger Instance? with which extra step ...
- c++线程传参问题
std::thread可以和任何可调用类型一起工作,可调用对象和函数带有参数时,可以简单地将参数传递给std::thread的构造函数 例如: #include<iostream> #in ...
- OC面向对象封装
面向对象语言的三大特性:封装.继承.多态 封装:不暴露自己类的内部的属性,提高自己的数据的安全性:就像一个接线盒一样,内部结构看不到,只有外部的接口提供给我们使用,这样既安全又美观:在代码方面就是结构 ...
- MITK Tutorial (三)
Step 2: Use the template with the plugins to read a image 在exampleplugin插件中QmitkAwesomeView.cpp中添加头文 ...
- 工程移除CocoaPods依赖库
http://zanderzhang.gitcafe.io/2015/09/26/工程移除CocoaPods依赖库/ 点这里--->CocoaPods安装和使用教程 当我们工程安装很多第三方开源 ...