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 ( ...
随机推荐
- 一、django rest_framework源码之总体流程剖析
1 序言 有如下django代码,视图层: from django.http import HttpResponse from rest_framework.views import APIView ...
- <泛> 并查集
最近写这些东西呢,主要是整理一下知识,自己写一遍,看看还是不是我的. 原理与实践相结合,缺一不可 背景 有时候,给你一张很复杂的关系网络图,如果关系具有传递性,那么,我们该如何处理这些关系集合. 一个 ...
- php操作mongodb or查询这样写!
$where['$or'] = [ ['id' => ['lt'=>0]], ['id2' => ['lt'=>1]] ]; 这个是查询 id>0 或者id2>1的 ...
- leetcode 无重复字符的最长子串 python实现
这道题需要借助哈希查找key的O(n) 时间复杂度, 否则就会超时 初始化一个 哈希表\字典 dic 头指针start 初始为0 当前指针 cur 初始为0 最大长度变量 l 初始为0 用cur变量 ...
- ARM 内核
ARM相关知识: ARM核:A8,ARM11,ARM9 指令架构:ARMv7,ARMv6,ARMv4 ARM核分为两个阵营: 经典型:ARM7,ARM9,ARM11 Cortex: Cortex A: ...
- CI Weekly #15 | 据说新版 flow.ci Dashboard 界面很酷
好久不见 :) 最近工程师们卯足了劲,全新的 flow.ci dashboard 页面 已经与所有用户见面了.更快捷地创建项目,构建列表页面新增分支,Pull Request 界面:侧边栏新增构建任务 ...
- Codeforces Round #358 (Div. 2) B. Alyona and Mex 水题
B. Alyona and Mex 题目连接: http://www.codeforces.com/contest/682/problem/B Description Someone gave Aly ...
- Git_分支管理
分支就是科幻电影里面的平行宇宙,当你正在电脑前努力学习Git的时候,另一个你正在另一个平行宇宙里努力学习SVN. 如果两个平行宇宙互不干扰,那对现在的你也没啥影响.不过,在某个时间点,两个平行宇宙合并 ...
- ESB的几个基本概念
京-星之泪: 请教一个问题:esb中路由和管道对的概念应该怎么理解,各自有什么用途,他们之间的关系 北京-kimmking: transport endpoint inbound outboun ...
- 计算机音频基础-PCM简介
我们在音频处理的时候经常会接触到PCM数据:它是模拟音频信号经模数转换(A/D变换)直接形成的二进制序列,该文件没有附加的文件头和文件结束标志. 声音本身是模拟信号,而计算机只能识别数字信号,要在计算 ...