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] ...
随机推荐
- hdu 6103(Kirinriki)
题目链接:Kirinriki 题目描述: 找两个不重叠的字符串A,B. 使得dis(A,B)<=m;\(dis(A,B)= \sum _{i=0}^{n-1} \left | A_i-B_{n- ...
- codeforces 609D D. Gadgets for dollars and pounds(二分+贪心)
题目链接: D. Gadgets for dollars and pounds time limit per test 2 seconds memory limit per test 256 mega ...
- POJ3417Network(LCA+树上查分||树剖+线段树)
Yixght is a manager of the company called SzqNetwork(SN). Now she's very worried because she has jus ...
- C# 获取Console的输入和输出 数据 (异步)
using System ; using System .Diagnostics; using System .IO; class Program { static void Main() ...
- CF-851B
B. Arpa and an exam about geometry time limit per test 2 seconds memory limit per test 256 megabytes ...
- HDU 1532 Drainage Ditches (网络流)
A - Drainage Ditches Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64 ...
- <c和指针>学习笔记1之快速上手和基本概念
1 c语言中的注释 功能:使这段代码在程序中不起作用,当然如果是功能注释,那是方便其他人阅读您的代码. 大部分情况下,多行的注释,我们采用的是这种方式,例如 /*内容*/. 这个符号不能嵌套,也就是 ...
- 7.19实习培训日志- java进阶
java进阶 java集合 Collection List ArrayList jdk1.2,异步处理,性能高,线程不安全 Vector jdk1.0,同步处理,性能低,线程安全 Set HashSe ...
- Python绘制正态分布曲线
使用Python绘制正态分布曲线,借助matplotlib绘图工具: \[ f(x) = \dfrac{1}{\sqrt{2\pi}\sigma}\exp(-\dfrac{(x-\mu)^2}{2 ...
- 计蒜课/UCloud 的安全秘钥(hash)
题目链接:https://nanti.jisuanke.com/t/15768 题意:中文题诶- 思路:直接hash就好了,当时zz了没想到... 代码: #include <iostream& ...