间隔问题,合并间隔(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 ...
随机推荐
- 关于小程序报错 缺少文件,错误信息:error: iconPath=../images/home.png, file not found
事实上在小程序中,虽然你的image文件夹是和你index文件夹的父级文件夹并行的文件夹,但是你如果引用的时候,不用去遵循 ../ 或者 ./因为在小程序当中他根本不识别.所以要引用的话 ...
- ffmpeg命令
1 将mp4格式的视频文件转换成mkv格式 ffmpeg -i input.mp4 -vcodec copy -acodec copy output.mkv
- IOS And WCF 上传文件
IOS And WCF Story 研究IOS上传到WCF图片的小功能,WCF实现服务端的文件上传的例子很多,单独实现IOS发送图片的例子也很多,但是两个结合起来的就很少了. 可以通过base64来上 ...
- 3*0.1 == 0.3 将会返回什么?true 还是 false?
false,因为有些浮点数不能完全精确的表示出来 public static void main(String[] args) { System.out.println(3 * 0.1); Syste ...
- 数据库字符集(AL32UTF8)和客户端字符集(2%)是不同的
登录oracle数据库时我们会遇到这样的提示信息:“数据库字符集(AL32UTF8)和客户端字符集(2%)是不同的”. 这是由于数据库服务端和客户端的字符集不一致所造成的,服务端字符集和客户端字符集相 ...
- Python 模块之 pyexcel_xls
一.适用场景 在很多数据统计或者数据分析的场景中,我们都会使用到excel: 在一些系统中我们也会使用excel作为数据导入和导出的方式,那么如何使用python加以辅助我们快速进行excel数据做更 ...
- 使用Ehcache缓存同步启动时抛出异常net.sf.ehcache.CacheException: Can't assign requested address
这个问题在插入公司内网网线的时候不会复现,由于我使用的是公司无线网络,故导致此问题. 具体解决办法是:在启动服务时,指定使用默认ipv4的网络接口.可以在启动jvm时添加参数-Djava.net.pr ...
- New Moto X 2014 全版本官方解锁Bootloader图文教程
]秒后松开,手机就会进入fastboot模式. 如下图: <ignore_js_op> 接下来,手机连接电脑,打开刚刚的fastboot工具里面的命令提示符: <ignore_js_ ...
- windows如何安装mysql
参考一下网址,已测试可用 https://www.cnblogs.com/reyinever/p/8551977.html
- Python(输入、输出;简单运算符;流程控制;转译)
一 输入输出 python3中统一都是input,python2中有raw_input等同于python3的input,另外python2中也有input 1.res=input("pyth ...