Merge Interval:

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].

算法分析:首先要对给定序列排序。然后再去遍历合并。

class Interval
{
int start;
int end;
Interval()
{
start = 0;
end = 0;
}
Interval(int s, int e)
{
start = s;
end = e;
}
}
public class MergeIntervals
{
public List<Interval> merge(List<Interval> intervals)
{
List<Interval> res = new ArrayList<>();
if(intervals == null || intervals.size() == 0)
{
return res;
}
Collections.sort(intervals, new Comparator<Interval>()//自定义比较方法,外部实现Comparator接口
{
public int compare(Interval i1, Interval i2)
{
if(i1.start != i2.start)
{
return i1.start - i2.start;
}
else
{
return i1.end - i2.end;
}
}
});
Interval pre = intervals.get(0);
for(int i = 0; i < intervals.size(); i ++)
{
Interval curr = intervals.get(i);
if(curr.start > pre.end)
{
res.add(pre);
pre = curr;
}
else
{
Interval merged = new Interval(pre.start, Math.max(pre.end, curr.end));
pre = merged;
}
}
res.add(pre);
return res;
}
}

Insert Interval:

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

You may assume that the intervals were initially sorted according to their start times.

Example 1:
Given intervals [1,3],[6,9], insert and merge [2,5] in as [1,5],[6,9].

Example 2:
Given [1,2],[3,5],[6,7],[8,10],[12,16], insert and merge [4,9] in as [1,2],[3,10],[12,16].

This is because the new interval [4,9] overlaps with [3,5],[6,7],[8,10].

给定间隔序列和插入间隔,默认给定间隔序列有序,返回合并后的不重叠的间隔序列。是上一道题的变形。

public class InsertInterval
{
public List<Interval> insert(List<Interval> intervals, Interval newInterval)
{
List<Interval> res = new ArrayList<>();
for (Interval interval : intervals)
{
if(interval.end < newInterval.start)
{
res.add(interval);
}
else if(interval.start > newInterval.end)
{
res.add(newInterval);
newInterval = interval;
}
else if(interval.start <= newInterval.end || interval.end >= newInterval.start)
{
newInterval = new Interval(Math.min(interval.start, newInterval.start),Math.max(interval.end, newInterval.end));
}
}
res.add(newInterval);
return res;
}
}

间隔问题,合并间隔(merge interval),插入间隔(insert interval)的更多相关文章

  1. 合并区间 · Merge Intervals & 插入区间 · Insert Interval

    [抄题]: 给出若干闭合区间,合并所有重叠的部分. 给出的区间列表 => 合并后的区间列表: [ [ [1, 3], [1, 6], [2, 6], => [8, 10], [8, 10] ...

  2. [Swift]LeetCode57. 插入区间 | Insert Interval

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

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

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

  4. leetCode 57.Insert Interval (插入区间) 解题思路和方法

    Insert Interval  Given a set of non-overlapping intervals, insert a new interval into the intervals ...

  5. leetcode 57 Insert Interval & leetcode 1046 Last Stone Weight & leetcode 1047 Remove All Adjacent Duplicates in String & leetcode 56 Merge Interval

    lc57 Insert Interval 仔细分析题目,发现我们只需要处理那些与插入interval重叠的interval即可,换句话说,那些end早于插入start以及start晚于插入end的in ...

  6. LeetCode 57. Insert Interval 插入区间 (C++/Java)

    题目: Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if nec ...

  7. 60. Insert Interval && Merge Intervals

    Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals ( ...

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

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

  9. .Net程序员学用Oracle系列(13):合并语句(MERGE)

    - 1.[**语法说明**](#segment1) - 1.1.[UPDATE 和 INSERT 可以只出现一个](#point11) - 1.2.[UPDATE 后面还可以再跟 WHERE](#po ...

随机推荐

  1. Vue入门之旅:一报错 Unknown ... make sure to provide the "name" option及error compiling template

    报错一: Unknown custom element: <custom-select> - did you register the component correctly? For r ...

  2. JavaScript表示x的y次幂

    一.指数运算符(**) 示例 console.log(2 ** 2); // 4 console.log(3 ** 2); // 9 console.log('3' ** '2'); // 9 con ...

  3. Java 之综合练习

    // 练习一: 写出程序结果 interface A{} class B implements A { public String func() { return "func"; ...

  4. 48dp rhythm

  5. Handler 与 Toast

    Toast或者Dialog中都有一个Handler的成员变量,所以如果不是在主线程中使用Toast或Dialog,则需要在使用Toast或者Dialog的线程中初始化Looper. Looper.pr ...

  6. Leetcode 之 Keys Keyboard

    1. 2 Keys Keyboard 先把dp的最小不走都设置为无穷大(Integer.MAX_VALUE),初始化条件:dp[0] = dp[1] = 0,状态转移方程为dp[i] = Math.m ...

  7. Python3+Selenium3自动化测试-(一)

    完成环境的安装并测试之后,我们对Selenium有了一定的了解了,接下来我们继续驱动浏览器做一些基本操作: 窗口尺寸设置.网页截图.刷新.前进和后退 窗口尺寸设置 在测试过程中,我们可能会要求打开浏览 ...

  8. java反射基础知识(四)反射应用实践

    反射基础 p.s: 本文需要读者对反射机制的API有一定程度的了解,如果之前没有接触过的话,建议先看一下官方文档的Quick Start. 在应用反射机制之前,首先我们先来看一下如何获取一个对象对应的 ...

  9. python全栈开发从入门到放弃之字符编码

    一 了解字符编码的知识储备   1. 计算机基础知识(三幅图)       2. 文本编辑器存取文件的原理(nodepad++,pycharm,word) 打开编辑器就打开了启动了一个进程,是在内存中 ...

  10. leetcode每日一题——两数之和

    题目: 两数之和 难度: 简单 描述: 给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用. 解法: class Solutio ...