【057-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].

题目大意

  给定一系列非覆盖的区间,插入一个新的区间。有必要的时候进行区间合并。区间開始是以起始时间进行合并的

解题思路

  假设原来的区间比插入区间小就插入结果集,假设插入区间有重叠,更新插入区间。假设插入区间小于原来的区间,先插入插入区间。再增加大的区间

代码实现

算法实现类

import java.util.LinkedList;
import java.util.List; public class Solution { public List<Interval> insert(List<Interval> intervals, Interval newInterval) { // 保存结果的集合
List<Interval> result = new LinkedList<>(); // 输入集非空
if (intervals != null) {
// 遍历元素
for (Interval item : intervals) {
// newInterval == null 表示插入的区间已经处理完了
// 将比插入区间小的区间增加结果集中
if (newInterval == null || item.end < newInterval.start) {
result.add(item);
}
// 将比插入区间大的区间增加结果集中,同一时候将插入的区间增加结果集
else if (item.start > newInterval.end) {
result.add(newInterval);
result.add(item);
newInterval = null;
}
// 插入区间有重叠,更新插入区间
else {
newInterval.start = Math.min(newInterval.start, item.start);
newInterval.end = Math.max(newInterval.end, item.end);
}
}
} // 假设插入区间非空说明插入区间还未被处理
if (newInterval != null) {
result.add(newInterval);
} return result;
}
}

评測结果

  点击图片,鼠标不释放。拖动一段位置。释放后在新的窗体中查看完整图片。

特别说明

欢迎转载,转载请注明出处【http://blog.csdn.net/derrantcm/article/details/47164431

【LeetCode-面试算法经典-Java实现】【057-Insert Interval(插入区间)】的更多相关文章

  1. 057 Insert Interval 插入区间

    给出一个无重叠的按照区间起始端点排序的区间列表.在列表中插入一个新的区间,你要确保列表中的区间仍然有序且不重叠(如果有必要的话,可以合并区间).示例 1:给定区间 [1,3],[6,9],插入并合并 ...

  2. leetCode 57.Insert Interval (插入区间) 解题思路和方法

    Insert Interval  Given a set of non-overlapping intervals, insert a new interval into the intervals ...

  3. LeetCode 57. Insert Interval 插入区间 (C++/Java)

    题目: Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if nec ...

  4. [LeetCode] Insert Interval 插入区间

    Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...

  5. [LeetCode] 57. Insert Interval 插入区间

    Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...

  6. [leetcode]57. Insert Interval插入区间

    Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...

  7. 【LeetCode-面试算法经典-Java实现】【139-Word Break(单词拆分)】

    [139-Word Break(单词拆分)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a string s and a dictionary of w ...

  8. 【LeetCode-面试算法经典-Java实现】【053-Maximum Subarray(最大子数组和)】

    [053-Maximum Subarray(最大子数组和)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Find the contiguous subarray w ...

  9. 【LeetCode-面试算法经典-Java实现】【062-Unique Paths(唯一路径)】

    [062-Unique Paths(唯一路径)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 A robot is located at the top-left c ...

随机推荐

  1. 在远程X server上显示图形的设置方法

    1.在服务器的/etc/ssh/sshd_config中,设置X11Forwarding yes,然后重启ssh服务,cd /etc/init.d这个目录下执行 ./ssh restart 2.在客户 ...

  2. Mybatis传递多个参数的解决办法(三种)

    第一种方案 DAO层的函数方法 Public User selectUser(String name,String area); 对应的Mapper.xml <select id="s ...

  3. K-D树学习笔记

    这东西其实就是高维二叉树?(反正我只会二维的) 大概就是把一个高维矩形按每一维分,一个点(及其子树)就表示一个高维区间,乱搞一下,就……没了? //BZOJ4066 "简单"题 / ...

  4. luogu P2252 取石子游戏(威佐夫博弈)

    题意 题解 对于像我这种不知道威佐夫博弈的人来说,拿到题就开始打表了. 然而打完后并没有发现什么. 然后才知道是威佐夫博弈. 结论是当(int)((b-a)*((sqrt(5.0)+1.0)/2.0) ...

  5. [luogu]P4364 [九省联考2018]IIIDX

    题目背景 Osu 听过没?那是Konano 最喜欢的一款音乐游戏,而他的梦想就是有一天自己也能做个独特酷炫的音乐游戏.现在,他在世界知名游戏公司KONMAI 内工作,离他的梦想也越来越近了. 这款音乐 ...

  6. 精品JS代码收藏大全

    1. oncontextmenu="window.event.returnvalue=false" 将彻底屏蔽鼠标右键 <table border oncontextmenu ...

  7. shuoj1936-D序列—最长上升子序列

    Description 已知两个长度为N的数组A和B.下标从0标号至N-1. 如今定义一种D序列 (如果长度为L).这样的序列满足下列条件: 1. 0 <= D[i] <= N-1 2.  ...

  8. nyoj914(二分搜索+贪心)

    题目意思: pid=914">acm.nyist.net/JudgeOnline/problem.php?pid=914 如今有n个物品的重量和价值各自是Wi和Vi,你能帮他从中选出k ...

  9. HDU 2112 HDU Today &lt;SPFA算法+map函数&gt;

    HDU Today Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  10. 里根上台时国债只占GDP的30%

    学里根是刻舟求剑,关键是钱从哪来 5  里根主要靠借钱,这是冷战红利,美国打完二战国债占了GDP的120%,然后总量就没怎么增加,但战后GDP快速增长,结果国债占GDP的比例连续下降,打越战登月石油危 ...