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 ...
随机推荐
- hadoop集群环境搭建准备工作
一定要注意hadoop和linux系统的位数一定要相同,就是说如果hadoop是32位的,linux系统也一定要安装32位的. 准备工作: 1 首先在VMware中建立6台虚拟机(配置默认即可).这是 ...
- windows下apache+php+mysql配置
Apache 2.4.10(文件:httpd-2.4.10-win64-VC11.zip) php 5.6.26 (文件:php-5.6.25-Win32-VC11-x64.zip) mysql 5. ...
- CSS Clip属性
Clip属性在W3C官网是这样进行描述的:“通过对元素进行剪切来控制元素的可显示区域,默认情况下,元素是不进行任何剪切的,但是也有可能剪切区域也显示的设置了clip属性”. .selector { c ...
- 在企业级开发中使用Try...Catch...会影响效率吗?
感谢神啊.上帝及老天爷让我失眠,才能够有了本篇文章. 记得不久之前,公司一同事曾经说过:“如果是Winform开发,由于程序是在本地,使用try...catch不会有太大性能问题,可是如果是在web服 ...
- mysqldump备份、还原数据库路径名含有空格的处理方法(如:Program Files)
虽然以下的方法也可以解决,不过最简单直接的,还是直接在路径前后加双引号-" ",这个方法简单有效. 首先要说明的是mysqldump.exe在哪里不重要,重要的是要处理好路径中的非 ...
- [转]mysql导出导入中文表解决方法
在开发过程中会经常用到mysql导出导入中文表,本文将详细介绍其如何使用,需要的朋友可以参考下. 在开发过程中会经常用到mysql导出导入中文表,本文将详细介绍其如何使用,需要的朋友可以参考下一.先针 ...
- windows API 统计系统字体
最近工作中遇到一个需求,需要统计当前系统中包含的所有字体.在网上逛了一圈后发现了EnumFontFamiliesEx这个API好像就可以实现这个功能.这里将自己对这个API的理解做一个记录,算是对这块 ...
- Qt Quick 与 QML语言(初学笔记1)
Qt Quick Qt Quick是一些新的UI技术的集合,用来帮助开发者创建一种现在越来越多用于手机.多媒体播放器.机顶盒以及其他便携式设备上的直观的.现代的.流畅的用户界面.简单来说,Qt Qui ...
- IOS下双击背景, touchmove, 阻止页面背景scroll.
ios prevent dblclick(tap) page scrollhtml add:("minimal-ui" is very important) <meta na ...
- Jquery异步请求数据实例
一.Jquery向aspx页面请求数据 前台页面JS代码: $("#Button1").bind("click", function () { $.ajax({ ...