057 Insert Interval 插入区间
给出一个无重叠的按照区间起始端点排序的区间列表。
在列表中插入一个新的区间,你要确保列表中的区间仍然有序且不重叠(如果有必要的话,可以合并区间)。
示例 1:
给定区间 [1,3],[6,9],插入并合并 [2,5] 得到 [1,5],[6,9].
示例 2:
给定区间 [1,2],[3,5],[6,7],[8,10],[12,16],插入并合并 [4,9] 得到 [1,2],[3,10],[12,16].
这是因为新的区间 [4,9] 与 [3,5],[6,7],[8,10] 重叠。
详见:https://leetcode.com/problems/insert-interval/description/
Java实现:
/**
* Definition for an interval.
* public class Interval {
* int start;
* int end;
* Interval() { start = 0; end = 0; }
* Interval(int s, int e) { start = s; end = e; }
* }
*/
class Solution {
public List<Interval> insert(List<Interval> intervals, Interval newInterval) {
List<Interval> res = new ArrayList<Interval>(); for(Interval each: intervals){
if(each.end < newInterval.start){
res.add(each);
}else if(each.start > newInterval.end){
res.add(newInterval);
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;
}
}
参考:https://www.cnblogs.com/springfor/p/3872333.html
057 Insert Interval 插入区间的更多相关文章
- leetCode 57.Insert Interval (插入区间) 解题思路和方法
Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals ...
- [LeetCode] Insert Interval 插入区间
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- [leetcode]57. Insert Interval插入区间
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- [LeetCode] 57. Insert Interval 插入区间
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- LeetCode 57. Insert Interval 插入区间 (C++/Java)
题目: Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if nec ...
- Java for LeetCode 057 Insert Interval
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 使用模拟 ...
- [Swift]LeetCode57. 插入区间 | Insert Interval
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- 合并区间 · Merge Intervals & 插入区间 · Insert Interval
[抄题]: 给出若干闭合区间,合并所有重叠的部分. 给出的区间列表 => 合并后的区间列表: [ [ [1, 3], [1, 6], [2, 6], => [8, 10], [8, 10] ...
随机推荐
- Linux下的Tomcat JVM 调优
1. 适用场景 Tomcat 运行过程遇到Caused by: java.lang.OutOfMemoryError: PermGen space或者java.lang.OutOfMemoryErro ...
- MySQL left join 20161024
公司OA系统上部门上线了一套流程,总部和分公司部门提数据需求都要走线上流程,审批,想想也是不错的,能和绩效更加合理的挂钩,还有打分评价,双向互动. 下午接到一个需求,查看某分公司上周订单使用优惠券情况 ...
- 「NOIP2013」「LuoguP1967」货车运输(最大生成树 倍增 LCA
题目描述 AA国有nn座城市,编号从 11到nn,城市之间有 mm 条双向道路.每一条道路对车辆都有重量限制,简称限重.现在有 qq 辆货车在运输货物, 司机们想知道每辆车在不超过车辆限重的情况下,最 ...
- 开源监控系统中 Zabbix 和 Nagios 哪个更好?
监控平台的话,各有优劣,但基本都可以满足需求.等达到一定监控指标后,发现,最困难的是监控项目的管理. CMDB中小规模(服务器<=1k):Zabbix大规模(1k>=服务器<=10k ...
- 10 Vue 学习 shortList页面
1: shortList页面代码如下: <template> <div class="fillcontain"> <head-top></ ...
- simple demo of Handlebars.js & jquery.js
simple demo of Handlebars.js & jquery.js <html> <head> <script src="jquery-1 ...
- Auto Layout Guide----(三)-----Anatomy of a Constraint
Anatomy of a Constraint 剖析约束 The layout of your view hierarchy is defined as a series of linear equa ...
- 要把target下面虚拟路径的项目文件…
源码进不去,要检查target下面的项目文件,要删除掉. 版权声明:本文为博主原创文章,未经博主允许不得转载.
- The Truth About GCHandles
I've heard several people asking why GCHandle doesn't implement IDisposable, considering it wraps an ...
- dialog 设置maxHeight 最大高度
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);Displ ...