[leetcode]57. 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:
Input: intervals = [[1,3],[6,9]], newInterval = [2,5] Output: [[1,5],[6,9]]
题目
给定一堆区间,插入一个新区间。
思路
代码
class Solution {
public List<Interval> insert(List<Interval> intervals, Interval newInterval) {
List<Interval> result = new ArrayList<Interval>();
for (Interval i : intervals) {
// non-overlapping | i |--|newInterval|
if (newInterval == null || i.end < newInterval.start){
result.add(i);
}
//non-overlapping |newInterval|--| i |
else if (i.start > newInterval.end) {
result.add(newInterval);
result.add(i);
newInterval = null;// guarantee not entring if (newInterval != null)
}
// overlapping, update start and end
else {
newInterval.start = Math.min(newInterval.start, i.start);
newInterval.end = Math.max(newInterval.end, i.end);
}
}
if (newInterval != null)
result.add(newInterval);
return result;
}
}
[leetcode]57. Insert Interval插入区间的更多相关文章
- LeetCode 57. Insert Interval 插入区间 (C++/Java)
题目: Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if nec ...
- leetCode 57.Insert Interval (插入区间) 解题思路和方法
Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals ...
- [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 (HARD)
Insert interval 题意简述:给定若干个数轴上的闭区间,保证互不重合且有序,要求插入一个新的区间,并返回新的区间集合,保证有序且互不重合. 只想到了一个线性的解法,所有区间端点,只要被其 ...
- 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] Insert Interval 插入区间
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- 057 Insert Interval 插入区间
给出一个无重叠的按照区间起始端点排序的区间列表.在列表中插入一个新的区间,你要确保列表中的区间仍然有序且不重叠(如果有必要的话,可以合并区间).示例 1:给定区间 [1,3],[6,9],插入并合并 ...
- Leetcode#57 Insert Interval
原题地址 遍历每个区间intervals[i]: 如果intervals[i]在newInterval的左边,且没有交集,把intervals[i]插入result 如果intervals[i]在ne ...
随机推荐
- Git 2.x 中git push时遇到 push.default 警告的解决方法
近在学习使用 git&GitHub,然后今天遇到了一个问题.在执行 git add 和 git commit 操作之后,再进行 git push 操作,出现了如下提示: $ git push ...
- EF 指定字段修改
public virtual void Modify(T model, params string[] ProNames) { DbEntityEntry entry = db.Entry<T& ...
- CSRF的防御解决过程
CSRF是什么,就不多说,网络上的帖子多的去了,关于其定义. 这里主要介绍我们项目中,是如何解决这个问题的.方案比较简单,重点是介绍和记录一下遇到的问题和一些小的心得. 1. 解决方案 A. 用户登录 ...
- Mvc项目实例 MvcMusicStore 五
Mvc项目实例 MvcMusicStore 一Mvc项目实例 MvcMusicStore 二Mvc项目实例 MvcMusicStore 三Mvc项目实例 MvcMusicStore 四Mvc项目实例 ...
- Android之微信布局篇
一.准备工作: 1. 下载好相关的图片: 2.创建一个名WeiChat的项目,将图片复制到res----->drawable-hdpi目录下. 二.编写代码: 1. 最终效果: 2.微信可划分为 ...
- 使用Java监控工具出现 Can't attach to the process
问题重现 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 ➜ jinfo -flags 3032 Attaching ...
- Python Day5 模块 包
一:区分Python文件的2种用途 1个Python文件的2种用途 1.1 当作脚本执行: if __name__ == '__main__': 1.2 当作模块导入使用 if ...
- 解决mysql使用GTID主从复制错误问题
做MySQL主从的话肯定会遇到很多同步上的问题,大多数都是由于机器宕机,重启,或者是主键冲突等引起的从服务器停止工作,这里专门收集类似问题并提供整理解决方案,仅供参考. 1.主从网络中断,或主服务器重 ...
- Java八大排序算法
Java八大排序算法: package sort; import java.util.ArrayList; import java.util.Arrays; import java.util.List ...
- rabbitmq (二) 持久化
默认情况下rabbitmq 是根据消费者多少依次投递,投递后就删除消息. 消息不会重复投递给不同的消费者. 消费者如果遇到长时间的任务,会执行完一个消息之后再执行下一个消息, 消费者持久化: 如果一个 ...