间隔问题,合并间隔(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 ...
随机推荐
- tomcat登录账户配置
tomcat7和tomcat6的用户信息配置有些不一样,tomcat7中添加了manager=gui和admin-gui角色,配置参考如下: 再 tomcat 文件夹的conf文件夹中的 tomcat ...
- C#操作AD及Exchange Server总结(二)
上一节C#操作AD及Exchange Server总结(一)写了对AD的操作,新建AD用户后,通常都需要为此用户开启Exchange邮箱,接下来写如何远程操作Exchange. 三.对Exchange ...
- ZOJ 3331 Process the Tasks(双塔DP)
Process the Tasks Time Limit: 1 Second Memory Limit: 32768 KB There are two machines A and B. T ...
- python抓取网页中的动态数据
一.概念 网页中的许多数据并不是写死在HTML中的,而是通过js动态载入的.所以也就引出了什么是动态数据的概念,动态数据在这里指的是网页中由Javascript动态生成的页面内容,是在页面加载到浏览器 ...
- python框架Scrapy中crawlSpider的使用——爬取内容写进MySQL
一.先在MySQL中创建test数据库,和相应的site数据表 二.创建Scrapy工程 #scrapy startproject 工程名 scrapy startproject demo4 三.进入 ...
- JAVA源码之JDK(一)——java.lang.Object
想要深入学习JAVA,还需追本溯源,从源码学起.这是我目前的想法.如今JAVA各种开源框架涌出,很多JAVA程序员都只停留在如何熟练使用的层次.身为其中一员的我深感惭愧,所以要加快学习的脚步,开始研究 ...
- 巨蟒python全栈开发-第3天
1 今日作业 1.有变量name = "aleX leNb" 完成如下操作: # 1)移除 name 变量对应的值两边的空格,并输出处理结果 '''''' ''' # name = ...
- 8.javascript获取表单中两个数字,并判断大小
获取表单数据的方式: 1.表单注意些id 2.使用document.getElementById("num").value 获取值 3.一定要注意现在得到是string类型,可以用 ...
- 传说中的MATLAB1.0(1984年DOS界面182K、运行良好)附下载
传说中的MATLAB1.0(1984年DOS界面182K.运行良好)附下载 你见过 MATLAB 最早的版本吗?下载一份 MATLAB 1.0,(只有182K, 只有20来个函数) 体会一下吧.MAT ...
- awesome-modern-cpp
Awesome Modern C++ A collection of resources on modern C++. The goal is to collect a list of resouce ...