[抄题]:

给出若干闭合区间,合并所有重叠的部分。

给出的区间列表 => 合并后的区间列表:

[                     [
[1, 3], [1, 6],
[2, 6], => [8, 10],
[8, 10], [15, 18]
[15, 18] ]
]

[暴力解法]:

时间分析:

空间分析:

[思维问题]:

[一句话思路]:

区间类问题,先把起点排序才能具有逐个合并的能力和性质

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. lambda表达式是调用了Comparator对象的.comparing方法进行的

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

[复杂度]:Time complexity: O(n) Space complexity: O(n)

[英文数据结构或算法,为什么不用别的数据结构或算法]:

List<Interval> ans = new LinkedList<Interval>(); 原来还有interval型的数据,只要实现了list能往后添加就行

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

495. Teemo Attacking

[代码风格] :

public class Solution {
/*
* @param intervals: interval list.
* @return: A new interval list.
*/
public List<Interval> merge(List<Interval> intervals) {
List<Interval> ans = new LinkedList<Interval>();
//sort
intervals.sort(Comparator.comparing(i -> i.start));//
//merge
Interval last = null;
for (Interval item : intervals) {
if (last == null || last.end < item.start) {
ans.add(item);
last = item;
}else {
last.end = Math.max(last.end, item.end);
}
}
return ans;
}
}

[抄题]:

给出一个无重叠的按照区间起始端点排序的区间列表。

在列表中插入一个新的区间,你要确保列表中的区间仍然有序且不重叠(如果有必要的话,可以合并区间)。

插入区间[2, 5] 到 [[1,2], [5,9]],我们得到 [[1,9]]

插入区间[3, 4] 到 [[1,2], [5,9]],我们得到 [[1,2], [3,4], [5,9]]

[暴力解法]:

一个个比较

时间分析:

空间分析:

[思维问题]:

先插入,再merge。followup能直接套就直接套,尽量减少改动。属于换一个描述,本质不变。

[一句话思路]:

先插入,再merge。

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

循环的对象是item,所以也对item进行插入

[复杂度]:Time complexity: O(n) Space complexity: O(n)

[英文数据结构或算法,为什么不用别的数据结构或算法]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

715. Range Module线段树

[代码风格] :

/**
* Definition of Interval:
* public classs Interval {
* int start, end;
* Interval(int start, int end) {
* this.start = start;
* this.end = end;
* }
*/ public class Solution {
/*
* @param intervals: Sorted interval list.
* @param newInterval: new interval.
* @return: A new interval list.
*/
public List<Interval> insert(List<Interval> intervals, Interval newInterval) {
List<Interval> ans = new LinkedList<>(); //insert
int ind = 0;
while (ind < intervals.size() && intervals.get(ind).start < newInterval.start) {
ind++;
}
intervals.add(ind, newInterval);
//merge
Interval last = null;
for (Interval item : intervals) {
if (last == null || last.end < item.start) {
ans.add(item);//
last = item;
}else {
last.end = Math.max(last.end, item.end);
}
} return ans;
}
}

合并区间 · Merge Intervals & 插入区间 · Insert Interval的更多相关文章

  1. leetcode 56. Merge Intervals 、57. Insert Interval

    56. Merge Intervals是一个无序的,需要将整体合并:57. Insert Interval是一个本身有序的且已经合并好的,需要将新的插入进这个已经合并好的然后合并成新的. 56. Me ...

  2. [Swift]LeetCode56. 合并区间 | Merge Intervals

    Given a collection of intervals, merge all overlapping intervals. Example 1: Input: [[1,3],[2,6],[8, ...

  3. 间隔问题,合并间隔(merge interval),插入间隔(insert interval)

    Merge Interval: Given a collection of intervals, merge all overlapping intervals. For example,Given  ...

  4. [leetcode]56. Merge Intervals归并区间

    Given a collection of intervals, merge all overlapping intervals. Example 1: Input: [[1,3],[2,6],[8, ...

  5. 【LeetCode-面试算法经典-Java实现】【057-Insert Interval(插入区间)】

    [057-Insert Interval(插入区间)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a set of non-overlapping in ...

  6. 【LeetCode】Merge Intervals 题解 利用Comparator进行排序

    题目链接Merge Intervals /** * Definition for an interval. * public class Interval { * int start; * int e ...

  7. 【题解】【区间】【二分查找】【Leetcode】Insert Interval & Merge Intervals

    Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...

  8. [LeetCode] Insert Interval 插入区间

    Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...

  9. [LeetCode] Merge Intervals 合并区间

    Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6],[8, ...

随机推荐

  1. crunch字典生成

    密码破解基本有三种方法:第一种是人工猜解(垃圾桶工程和被动信息收集): 第二种是基于字典暴力破解(主流) 在kali里,是默认自带了字典的,分别放在下面三个文件中:/usr/share/wordlis ...

  2. Android ffmpeg rtmp(source code)

    souce code: Android.mk 编译生成APK需要调用的so文件 LOCAL_PATH:= $(call my-dir) include $(CLEAR_VARS) LOCAL_MODU ...

  3. var与dynamic

    var与dynamic 如果你用MVC写过程序,那么你应该知道ViewBag这个用于前后台的数据传递工具,那么你是否对ViewBag的用法感到过疑惑呢? ViewBag.Mode1l=new obje ...

  4. redis入门之jedis

    jedis是redis官方首选的java客户端开发包 开源托管地址:https://github.com/xetorthio/jedis 下载地址,以及maven, 依赖参考: 下面来编写一段程序进行 ...

  5. float属性详解

    内容: 1.block与inline复习 2.float介绍 3.float作用 4.清除浮动 1.block与inline复习 1 block元素是独立的一块,独占一行 2 多个block元素会各自 ...

  6. 系统一般信息监控查看shell.磁盘,负载等达阀值告警机制,改进测试中.

    1 #!/bin/sh  2 #Create by Qrui  3 while [ "1"="1" ]  4 do  5 clear  6  7 echo &q ...

  7. delphi XE7 判断手机返回键

    Using the Android Device's Back Button To make your application handle when users press the Back but ...

  8. favicon.ico

    favicon.ico 作为网页的图标,被当前的所有浏览器都支持. 可直接放在主目录下,自动加载,也可设置在header中. <link rel="shortcut icon" ...

  9. mtr 命令详解

    一般在windows 来判断网络连通性用ping 和tracert,ping的话可以来判断丢包率,tracert可以用来跟踪路由,在Linux中有一个更好的网络连通性判断工具,它可以结合ping ns ...

  10. 分页导航jsp

    <c:choose>标签与Java switch语句的功能一样,用于在众多选项中做出选择. switch语句中有case,而<c:choose>标签中对应有<c:when ...