间隔问题,合并间隔(merge interval),插入间隔(insert interval)
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)的更多相关文章
- 合并区间 · Merge Intervals & 插入区间 · Insert Interval
[抄题]: 给出若干闭合区间,合并所有重叠的部分. 给出的区间列表 => 合并后的区间列表: [ [ [1, 3], [1, 6], [2, 6], => [8, 10], [8, 10] ...
- [Swift]LeetCode57. 插入区间 | Insert Interval
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- leetcode 56. Merge Intervals 、57. Insert Interval
56. Merge Intervals是一个无序的,需要将整体合并:57. Insert Interval是一个本身有序的且已经合并好的,需要将新的插入进这个已经合并好的然后合并成新的. 56. Me ...
- leetCode 57.Insert Interval (插入区间) 解题思路和方法
Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals ...
- 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 ...
- LeetCode 57. Insert Interval 插入区间 (C++/Java)
题目: Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if nec ...
- 60. Insert Interval && Merge Intervals
Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals ( ...
- 【题解】【区间】【二分查找】【Leetcode】Insert Interval & Merge Intervals
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- .Net程序员学用Oracle系列(13):合并语句(MERGE)
- 1.[**语法说明**](#segment1) - 1.1.[UPDATE 和 INSERT 可以只出现一个](#point11) - 1.2.[UPDATE 后面还可以再跟 WHERE](#po ...
随机推荐
- 延迟任务和循环任务ScheduledExecutorService
public class ScheduledThreadPool { public static ScheduledExecutorService scheduledThreadPool = Exec ...
- java的static final和final的区别
转自:https://www.cnblogs.com/EasonJim/p/7841990.html 说明:不一定准确,但是最快理解. final: final可以修饰:属性,方法,类,局部变量(方法 ...
- JavaWeb开发的一系列可能的配置
最近电脑硬盘坏了Orz...,重装了.唉!软件什么的都要重新装,重新设置,好麻烦! 我决定写下重装javaweb开发环境的过程 1.Java的安装,配置 https://www.runoob.com/ ...
- 第一份PHP程序
<?php list($ncase) = fscanf(STDIN,"%d"); $mod = 1000000007; for($n=0;$n<$ncase;++$n) ...
- app返回之前app焦点的操作方法
var hdWin,hdfocus: THandle; trdID: Cardinal; //获取前置app窗口句柄 hdWin := GetForegroundWindow;//FindWindow ...
- js中window.open的参数及注意
IE9下使用window.open时需要注意name参数值不能有"-"出现,否则会出现脚本错误,IE9以及版本测试没有问题 window.open(URL,name,specs ...
- PyMongo的使用(转)
原文:http://www.oschina.net/code/snippet_1382328_37407 #!/usr/bin/env python #coding:utf-8 # Author: - ...
- python中得到shell命令输出的方法
python中得到shell命令输出的方法: 1. import subprocess output = subprocess.Popen(['ls','-l'],stdout=subprocess ...
- PHP逐字符读取数据
<?php $file = fopen("Minot.txt", "r") or exit("Unable to open file!" ...
- iOS学习之移除Main.storyboard
每次使用Single View Application模板创建工程之后,总是会有一个Main.storyboard文件,那么,当我们使用代码布局的时候,很显然是不需要它的.那么,如何将它从工程中移除呢 ...