Insert Intervals

Given a non-overlapping interval list which is sorted by start point.

Insert a new interval into it, make sure the list is still in order and non-overlapping (merge intervals if necessary).

Example

Insert [2, 5] into [[1,2], [5,9]], we get [[1,9]].

Insert [3, 4] into [[1,2], [5,9]], we get [[1,2], [3,4], [5,9]].

分析:

Create a new array list, insert the smaller interval in the new array and use a counter to keep track of the total number of smaller intervals. If we find an interval overlapping with the new one, we need to change the start and end.

 class Solution {
public int[][] insert(int[][] intervals, int[] newInterval) {
List<int[]> resultList = new ArrayList<>();
boolean hasInserted = false;
for (int[] interval : intervals) {
if (interval[] > newInterval[]) {
if (!hasInserted) {
resultList.add(newInterval);
hasInserted = true;
}
resultList.add(interval);
} else if (interval[] < newInterval[]) {
resultList.add(interval);
} else {
newInterval[] = Math.min(newInterval[], interval[]);
newInterval[] = Math.max(newInterval[], interval[]);
}
} if (!hasInserted) {
resultList.add(newInterval);
} return resultList.toArray(new int[][]);
}
}

Merge Intervals

Given a collection of intervals, merge all overlapping intervals.

Example

Given intervals => merged intervals:

[                     [
[1, 3], [1, 6],
[2, 6], => [8, 10],
[8, 10], [15, 18]
[15, 18] ]
] Analyze: Sort first, then merge intervals if they overlap.
 /**
* 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; }
* }
*/
public class Solution {
public List<Interval> merge(List<Interval> list) {
if (list == null || list.size() <= ) return list; // Collections.sort(list, (Interval a, Interval b) -> a.start - b.start);
list.sort((i1, i2) -> Integer.compare(i1.start, i2.start)); for (int i = ; i < list.size(); i++) {
if (overlap(list.get(i), list.get(i - ))) {
list.get(i - ).start = Math.min(list.get(i).start, list.get(i - ).start);
list.get(i - ).end = Math.max(list.get(i).end, list.get(i - ).end);
list.remove(i);
i--;
}
}
return list;
} boolean overlap(Interval i1, Interval i2) {
return Math.max(i1.start, i2.start) <= Math.min(i1.end, i2.end);
}
}
 

Insert Interval & Merge Intervals的更多相关文章

  1. 60. Insert Interval && Merge Intervals

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

  2. 【题解】【区间】【二分查找】【Leetcode】Insert Interval & Merge Intervals

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

  3. 56. Merge Intervals 57. Insert Interval *HARD*

    1. Merge Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[ ...

  4. 合并区间 · Merge Intervals & 插入区间 · Insert Interval

    [抄题]: 给出若干闭合区间,合并所有重叠的部分. 给出的区间列表 => 合并后的区间列表: [ [ [1, 3], [1, 6], [2, 6], => [8, 10], [8, 10] ...

  5. [LeetCode] Merge Interval系列,题:Insert Interval,Merge Intervals

    Interval的合并时比较常见的一类题目,网上的Amazon面经上也有面试这道题的记录.这里以LeetCode上的例题做练习. Merge Intervals Given a collection ...

  6. leetcode 56. Merge Intervals 、57. Insert Interval

    56. Merge Intervals是一个无序的,需要将整体合并:57. Insert Interval是一个本身有序的且已经合并好的,需要将新的插入进这个已经合并好的然后合并成新的. 56. Me ...

  7. Leetcode: Merge/Insert Interval

    题目 Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[ ...

  8. 间隔问题,合并间隔(merge interval),插入间隔(insert interval)

    Merge Interval: Given a collection of intervals, merge all overlapping intervals. For example,Given  ...

  9. 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 ...

随机推荐

  1. Linux内核分析作业第二周

    操作系统是如何工作的 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 一.函数调用堆栈 1.计算机工作三 ...

  2. oracle (+) 什么意思

    oracle中的(+)是一种特殊的用法,(+)表示外连接,并且总是放在非主表的一方. 例如左外连接:select A.a,B.a from A LEFT JOIN B ON A.b=B.b;等价于se ...

  3. 转Git学习碰到的问题

    [原]Git学习碰到的问题 1.通过 windows 下的 gitbash 连接 github 时 $ ssh-keygen -t rsa -C "abcd@efgh.com" / ...

  4. js 复制到剪切板

    function copyTextToClipboard(text) { var copyFrom = $('<textarea/>'); copyFrom.text(text); $(' ...

  5. 使用spring单元调试出错initializationError

    1.引入spring jar包之后,单元测试一直出错,提示initializationError. 2.在网上看说是junit版本的问题,引入其他版本之后,还是不对. 3.最后发现是需要引入 这两个j ...

  6. mybatis之一对一关联

    MapperAsso.xml <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper ...

  7. oracle 创建表空间 与创建用户与分配用户权限

    创建一个表空间名为ABC create tablespace "ABC"     //貌似要大写 datafile 'D:\oracle\TBSPACES\ABC.dbf'   / ...

  8. MT【178】平移不变性

    (2008年北大自招)已知$a_1,a_2,a_3;b_1,b_2,b_3$满足$a_1+a_2+a_3=b_1+b_2+b_3$$a_1a_2+a_2a_3+a_3a_1=b_1b_2+b_2b_3 ...

  9. suoi44 核能显示屏 (cdq分治)

    首先二维树状数组肯定开不下 仿照二维树状数组的做法,如果有差分数组$d[i][j]=a[i][j]-a[i-1][j]-a[i][j-1]+a[i-1][j-1]$,那么就有: $$sum[x][y] ...

  10. 进程和线程(3)-ThreadLocal

    ThreadLocal 在多线程环境下,每个线程都有自己的数据.一个线程使用自己的局部变量比使用全局变量好,因为局部变量只有线程自己能看见,不会影响其他线程,而全局变量的修改必须加锁. 但是局部变量也 ...