Insert Interval leetcode java
题目:
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]
.
题解:
这道题不仅要insert newInterval同时还要保证能够merge。那么就分情况讨论。
遍历每一个已给出的interval,
当当前的interval的end小于newInterval的start时,说明新的区间在当前遍历到的区间的后面,并且没有重叠,所以res添加当前的interval;
当当前的interval的start大于newInterval的end时,说明新的区间比当前遍历到的区间要前面,并且也没有重叠,所以把newInterval添加到res里,并更新newInterval为当前的interval;
当当前的interval与newInterval有重叠时,merge interval并更新新的newInterval为merge后的。
代码如下:
1 public ArrayList<Interval> insert(ArrayList<Interval> intervals, Interval newInterval) {
2 ArrayList<Interval> res = new ArrayList<Interval>();
3
4 for(Interval each: intervals){
5 if(each.end < newInterval.start){
6 res.add(each);
7 }else if(each.start > newInterval.end){
8 res.add(newInterval);
9 newInterval = each;
}else if(each.end >= newInterval.start || each.start <= newInterval.end){
int nstart = Math.min(each.start, newInterval.start);
int nend = Math.max(newInterval.end, each.end);
newInterval = new Interval(nstart, nend);
}
}
res.add(newInterval);
return res;
}
Reference://http://www.programcreek.com/2012/12/leetcode-insert-interval/
Insert Interval leetcode java的更多相关文章
- 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 ...
- Merge Interval leetcode java
题目: Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6] ...
- Search Insert Position leetcode java
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- 【题解】【区间】【二分查找】【Leetcode】Insert Interval & Merge Intervals
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- leetcode Insert Interval 区间插入
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4051169.html 题目链接:leetcode Insert Interval 使用模拟 ...
- [LeetCode] Merge Interval系列,题:Insert Interval,Merge Intervals
Interval的合并时比较常见的一类题目,网上的Amazon面经上也有面试这道题的记录.这里以LeetCode上的例题做练习. Merge Intervals Given a collection ...
- 【LeetCode】57. Insert Interval [Interval 系列]
LeetCode中,有很多关于一组interval的问题.大体可分为两类: 1.查看是否有区间重叠: 2.合并重叠区间; 3.插入新的区间: 4. 基于interval的其他问题 [ 做题通用的关键 ...
- 【leetcode】Insert Interval
Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals ( ...
- 【LeetCode】57. Insert Interval
Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals ( ...
随机推荐
- 解决命令行运行python文件,出现No module named *** 报错问题
有时候在一个项目中运行的时候,可能是之前已经mark成sources root 你自己忘记了, 于是就在命令行也执行python文件,然后就出现 No module named *** 等 相关你认为 ...
- 洛谷P3265 [JLOI2015]装备购买 [线性基]
题目传送门 装备购买 格式难调,题面就不放了. 分析: 一句话,有$n$件物品,每件物品有$m$个属性和一个花费值,如果一个装备的属性值可以由其他装备的属性值改变系数后组合得到那就不买,求购买最多装备 ...
- 【记录】HTTP协议状态码含义
状态码200-299之间的状态码表示成功300-399之间的代码表示资源已经被移走400-499之间的代码表示客户端的请求出错500-599之间的代码表示服务器出错了
- 自然语言处理系列-4条件随机场(CRF)及其tensorflow实现
前些天与一位NLP大牛交流,请教其如何提升技术水平,其跟我讲务必要重视“NLP的最基本知识”的掌握.掌握好最基本的模型理论,不管是对日常工作和后续论文的发表都有重要的意义.小Dream听了不禁心里一颤 ...
- CF687B Remainders Game
题意:已知n个数,第i个为ci,给定一个数x mod ci的结果,再给点一个k,问能不能知道x mod k的值? 分析:刚看题目的我一脸蒙蔽,对题意有点不理解,能的情况似乎有很多,我该从哪里下手呢 ...
- sort change from to range
SORT FIELDS=COPY OUTREC FINDREP=(IN=X'05',OUT=X'40', STARTPOS=107,ENDPOS=204)
- iOS 9应用开发教程之多行读写文本ios9文本视图
iOS 9应用开发教程之多行读写文本ios9文本视图 多行读写文本——ios9文本视图 文本视图也是输入控件,与文本框不同的是,文本视图可以让用户输入多行,如图2.23所示.在此图中字符串“说点什么吧 ...
- apt-get出现无法定位安装包问题解决
这个问题出现在sources.list上 编辑/etc/apt/sources.list下的文件 找到检查你的存储库是否正确 你可以在以下页面找到不同版本 Kali Linux 的存储库:http:/ ...
- [COGS2639]偏序++
[COGS2639]偏序++ 题目大意: \(n(n\le40000)\)个\(k(k\le7)\)元组,求\(k\)维偏序. 思路: 分块后用bitset维护. 时间复杂度\(\mathcal O( ...
- bzoj 3611
和BZOJ消耗站一样,先将那个询问的简图构建出来,然后就是简单的树形DP. (倍增数组开小了,然后就狂WA,自己生成的极限数据深度又没有那么高,链又奇迹般正确) #include <cstdio ...