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

解题思路一:

参考Java for LeetCode 056 Merge Intervals思路一,去掉最外层循环即可,JAVA实现如下:

public List<Interval> insert(List<Interval> intervals, Interval newInterval) {
if (newInterval == null)
return intervals;
int startIndex = 0, endIndex = 0;
for (int j = 0; j < intervals.size(); j++) {
if (newInterval.start > intervals.get(j).end) {
startIndex += 2;
endIndex += 2;
continue;
}
if (newInterval.end < intervals.get(j).start)
break;
if (newInterval.start >= intervals.get(j).start)
startIndex++;
if (newInterval.end > intervals.get(j).end) {
endIndex += 2;
continue;
}
if (newInterval.end >= intervals.get(j).start)
endIndex++;
break;
}
if(startIndex==endIndex&&startIndex%2==0)
intervals.add(startIndex/2,new Interval(newInterval.start,newInterval.end));
else if(startIndex%2==0&&endIndex%2==0){
intervals.get(startIndex/2).start=newInterval.start;
intervals.get(startIndex/2).end=newInterval.end;
for(int k=1;k<endIndex/2-startIndex/2;k++)
intervals.remove(startIndex/2+1);
}
else if(startIndex%2==0&&endIndex%2!=0){
intervals.get(startIndex/2).start=newInterval.start;
intervals.get(startIndex/2).end=intervals.get(endIndex/2).end;
for(int k=1;k<=endIndex/2-startIndex/2;k++)
intervals.remove(startIndex/2+1);
}
else if(startIndex%2!=0&&endIndex%2==0){
intervals.get(startIndex/2).end=newInterval.end;
for(int k=1;k<endIndex/2-startIndex/2;k++)
intervals.remove(startIndex/2+1);
}
else if(startIndex%2!=0&&endIndex%2!=0){
intervals.get(startIndex/2).end=intervals.get(endIndex/2).end;
for(int k=1;k<=endIndex/2-startIndex/2;k++)
intervals.remove(startIndex/2+1);
}
return intervals;
}

解题思路二:

参考Java for LeetCode 056 Merge Intervals思路二,添加后重新排序即可,JAVA实现如下:

	public List<Interval> insert(List<Interval> intervals, Interval newInterval) {
if (intervals == null)
return intervals;
List<Interval> list = new ArrayList<Interval>();
intervals.add(newInterval);
Comparator<Interval> comparator = new Comparator<Interval>() {
@Override
public int compare(Interval o1, Interval o2) {
if (o1.start == o2.start)
return o1.end - o2.end;
return o1.start - o2.start;
}
};
Collections.sort(intervals, comparator);
for (Interval interval : intervals) {
if (list.size() == 0 || list.get(list.size() - 1).end < interval.start)
list.add(new Interval(interval.start, interval.end));
else
list.get(list.size() - 1).end = Math.max(interval.end, list.get(list.size() - 1).end);
}
return list;
}

Java for LeetCode 057 Insert Interval的更多相关文章

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

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

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

  3. 【leetcode】Insert Interval

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

  4. Leetcode: Merge/Insert Interval

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

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

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

  6. 第一周 Leetcode 57. Insert Interval (HARD)

    Insert interval  题意简述:给定若干个数轴上的闭区间,保证互不重合且有序,要求插入一个新的区间,并返回新的区间集合,保证有序且互不重合. 只想到了一个线性的解法,所有区间端点,只要被其 ...

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

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

  8. LeetCode: 57. Insert Interval(Hard)

    1. 原题链接 https://leetcode.com/problems/insert-interval/description/ 2. 题目要求 该题与上一题的区别在于,插入一个新的interva ...

  9. 057 Insert Interval 插入区间

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

随机推荐

  1. 【HDU 2604】Queuing

    题 题意 f和m两种字母组成字符串,fmf 和 fff 这种为不安全的字符串,现在有2*L个字母,问你有多少安全的字符串.答案mod M. 分析 递推,这题本意是要用矩阵快速幂.不过我发现这题好神奇, ...

  2. BIEE 维表

    (1)       在物理层给表创建别名(表——>新建对象——>别名) (1)       在业务层创建维度(表——>创建逻辑维——>基于级别层次的维) 钻取是维本身的功能 一 ...

  3. HDU 1060 Left-most Digit

    传送门 Leftmost Digit Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  4. PL/0编译器(java version) - MainFrame.java

    1: /* 2: * To change this license header, choose License Headers in Project Properties. 3: * To chan ...

  5. Oracle DBA从小白到入职实战应用

    现如今Oracle依然是RDBMS的王者,在技术上和战略上,Oracle仍然一路高歌猛进,并且全面引领行业迈入了云时代,伴随着12cR2即将在2016年正式发布,学习Oracle之路依旧任重道远,目前 ...

  6. C#实现自动升级(附源码)

    http://blog.csdn.net/zhuweisky/article/details/50439386 OAUS

  7. Spring MVC GET 从客户端数据到服务器端的乱码和服务器端数据到客户端的乱码

    参考资料:http://m.oschina.net/blog/376339 乱码的本质是涉及到编解码的几个过程所用的编码方式不一样. 一.从服务端到客户端 在整个服务器端数据返回到浏览器的过程中,涉及 ...

  8. Machine Learning Methods: Decision trees and forests

    Machine Learning Methods: Decision trees and forests This post contains our crib notes on the basics ...

  9. 锋利的jQuery-4--trigger()和triggerHandler()

    trigger()方法触发事件后,会执行浏览器默认操作. $("input").trigger("focus") 以上的代码不仅会执行input绑定的focus ...

  10. jQuery中prop()函数控制多选框(全选,反选)

    今天看了jQuery手册,对prop()函数又多了一点认识,记忆力不好,记录下来. prop() : 获取匹配元素集中第一个元素的值 判断checkbox中的第一个是否被选中: $(":ch ...