Merge Intervals——LeetCode
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排序,然后开始循环,判断当前end和下一个的start是否有overlap,没有的话,可以输出当前这个区间,有的话把后一个的end设置为max{后一个end,当前end},循环到最后,把所有符合条件的都加到res列表里。
public static List<Interval> merge(List<Interval> it) {
List<Interval> res = new ArrayList<>();
if (it == null || it.size() == 0) {
return res;
}
Comparator<Interval> comp = new Comparator<Interval>() {
@Override
public int compare(Interval o1, Interval o2) {
if (o1.start - o2.start != 0) {
return o1.start - o2.start;
}
return o2.end - o1.end;
}
};
Collections.sort(it, comp);
int begin = it.get(0).start;
int end = it.get(0).end;
for (int i = 0; i < it.size() - 1; i++) {
if (it.get(i).end < it.get(i + 1).start) {
res.add(new Interval(begin, it.get(i).end));
begin = it.get(i + 1).start;
} else {
it.get(i + 1).end = Math.max(it.get(i).end, it.get(i + 1).end);
}
end = it.get(i + 1).end; }
res.add(new Interval(begin, end));
return res;
}
看到别人还有更高效的方案,直接遍历源List,在上面操作,不需要new新的Interval,本来想用Java8的lambada表达式写Comparator的,但是不知道为什么在提交的时候总是TLE。
public static List<Interval> merge(List<Interval> it) {
List<Interval> res = new ArrayList<>();
if (it == null || it.size() == 0) {
return res;
}
//这里如果用lambda表达式,会超时
//Comparator<Interval> comp = (o1, o2) -> o1.start - o2.start;
Comparator<Interval> comp = new Comparator<Interval>() {
@Override
public int compare(Interval o1, Interval o2) {
return o1.start - o2.start;
}
};
Collections.sort(it, comp);
Interval curr;
Iterator<Interval> itor = it.iterator();
curr = itor.next();
while (itor.hasNext()) {
Interval tmp = itor.next();
if (curr.end >= tmp.start) {
curr.end = Math.max(curr.end, tmp.end);
itor.remove();
} else {
curr = tmp;
}
}
return res;
}
Merge Intervals——LeetCode的更多相关文章
- Merge Intervals - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Merge Intervals - LeetCode 注意点 区间是无序的 每个区间start一定小于end 解法 解法一:首先以start的值从小到大来 ...
- 56. Merge Intervals - LeetCode
Question 56. Merge Intervals Solution 题目大意: 一个坐标轴,给你n个范围,把重叠的范围合并,返回合并后的坐标对 思路: 先排序,再遍历判断下一个开始是否在上一个 ...
- 【题解】【区间】【二分查找】【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. ...
- LeetCode: Merge Intervals 解题报告
Merge IntervalsGiven a collection of intervals, merge all overlapping intervals. For example,Given [ ...
- [LeetCode] Merge Interval系列,题:Insert Interval,Merge Intervals
Interval的合并时比较常见的一类题目,网上的Amazon面经上也有面试这道题的记录.这里以LeetCode上的例题做练习. Merge Intervals Given a collection ...
- [Leetcode Week2]Merge Intervals
Merge Intervals题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/merge-intervals/description/ Descript ...
- 【LeetCode】Merge Intervals 题解 利用Comparator进行排序
题目链接Merge Intervals /** * Definition for an interval. * public class Interval { * int start; * int e ...
- 【leetcode】Merge Intervals
Merge Intervals Given a collection of intervals, merge all overlapping intervals. For example,Given ...
随机推荐
- PHP替换数据库的换行符
//php 有三种方法来解决 //1.使用str_replace 来替换换行 $str = str_replace(array("\r\n", "\r", &q ...
- iBatis 的简单入门
iBATIS一词来源于“internet”和“abatis”的组合,于2010年6月16号被谷歌托管,改名为MyBatis.是一个基于SQL映射支持Java和·NET的持久层框架. ibatis本是a ...
- mysql数据库优化日志(更)-howyue
1)记一次首页查询优化 优化前: 优化后: 主要优化: 1.select查询只查询需要字段: 2.where条件字段添加索引:
- StudioStyle 使用 厌倦了默认的Visutal Studio样式了,到这里找一个酷的试试
厌倦了默认的Visutal Studio样式了,到这里找一个酷的试试 http://studiostyl.es/ 去下载个自己喜欢的编码样式吧 如果你有想法 有能力 可以自己去做一个自己喜欢的 OK ...
- 微信小程序开发之入门篇(熟悉开发工具)
个人的每一篇博文都谈不上有什么技术含量,只是为了帮助不熟悉微信小程序开发的自己及他人提供一下思路.谢谢,下面开始! PS: 因为本人没有小程序的内测资格,所以所有的开发及Demo都是无AppId的,如 ...
- mysql 数据sqoop到hive 步骤
1.hive建表 hive是支持分区的,但是这次建表没有写分区. CREATE TABLE `cuoti_rpt` ( `COURSE_ID` string, `NAME` string, `PERI ...
- Proxy 模式
在以下集中情况下可以用 Proxy模式解决问题: 1)创建开销大的对象时候,比如显示一幅大的图片,我们将这个创建的过程交给代理去完成,GoF 称之为虚代理(Virtual Proxy): 2)为网络上 ...
- MVC Unit Testing学习笔记
MVC Unit Testing 参考文档: 1.http://www.asp.net/mvc/overview/testing 2.http://www.asp.net/mvc/tutorials/ ...
- 利用C#的反射机制动态调用DLL类库
最近由于业务要求,需要动态调用DLL类库,所以研究了一下,感觉还好也不太难,今天就把自己理解的写了一个小例子(已经通过VS2005跑通),供大家一起研究和探讨,有理解不当的地方还请高手们多多指正,谢谢 ...
- 【技术宅3】截取文件和url扩展名的N种方法
//截取文件扩展名的N种方法 //第1种 //strrchr() 函数查找字符在指定字符串中最后一次出现的位置,如果成功,则返回其后面的字符串 //返回带有点的扩展名 function get_e ...