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. 使用maven搭建SSM框架

    使用maven搭建SSM框架,首先得准备好maven环境. 搭建maven环境 第一步:下载maven http://maven.apache.org/download.cgi 下载后解压就可以了. ...

  2. 《挑战程序设计竞赛》2.3 动态规划-进阶 POJ1065 1631 3666 2392 2184(5)

    POJ1065: Description There is a pile of n wooden sticks. The length and weight of each stick are kno ...

  3. Backtracking is a form of recursion.

    w https://www.cis.upenn.edu/~matuszek/cit594-2012/Pages/backtracking.html Starting at Root, your opt ...

  4. 如何在 window 上面输入特殊字符?

    打开 字符映射表 程序 选中任意一个字符,它会在下方显示该字符的 16进制 转换16进制至10进制,并在输入法打开的状态下,按住 Alt 键输入 10 进制数值即可.

  5. 【Linux command reference】

    ubuntu16.04安装中文输入法: https://blog.csdn.net/singleyellow/article/details/77448246 ubuntu16.04 用vi编辑代码, ...

  6. git 解决push报错:[rejected] master -> master (fetch first) error: failed to push some refs to

    今天对代码进行了修改优化,然后往往远程push,但push后报错了 git操作 git add . git commit -m"fix" git push origin maste ...

  7. Kotlin 初级读本

    第一部分——快速上手第一章·启程 第二章·基本语法第三章·Kotlin 与 Java 混编 第二部分——开始学习 Kotlin第四章·Kotlin 的类特性(上)第四章·Kotlin 的类特性(下)第 ...

  8. 我们是80后 golang入坑系列

    现在这个系列,已经开始两极分化了. 点赞的认为风格轻松,看着不困.反之,就有人嫌写的罗里吧嗦,上纲上线.所以善意提醒,里面不只是技术语言,还有段子.专心看技术的,千万别点!别怪我没提醒!差点忘说,版权 ...

  9. django的序列化问题

    Django中的序列化主要应用在将数据库中检索的数据返回给客户端用户,特别的Ajax请求一般返回的为Json格式 1.serializers from django.core import seria ...

  10. 关于shared pool的深入探讨(六)-高Latch竞争案例

    研究了几天shared pool,没想到忽然就撞到问题上来了.作为一个案例写出来给大家参考一下吧. 问题起因是公司做短信群发,就是那个18万买的4000字的短信小说(嘘,小声点,我也没看过...).群 ...