Insert Interval

原创 2015年07月09日 22:40:35

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

/**
 * 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> insert(List<Interval> intervals, Interval newInterval) {
        List<Interval> res = new ArrayList<Interval>();
        if (intervals.size() == 0) {
            intervals.add(newInterval);
            return intervals;
        }
        
        
        int i = 0;
        while (i < intervals.size() && intervals.get(i).end < newInterval.start) {
            res.add(intervals.get(i));
            i++;
        }
        
        if (i < intervals.size()) {
            newInterval.start = Math.min(intervals.get(i).start, newInterval.start);
        }
        
        res.add(newInterval);
        
        while (i < intervals.size() && intervals.get(i).start <= newInterval.end) {
            newInterval.end = Math.max(newInterval.end, intervals.get(i).end);
            i++;
        }
        while(i < intervals.size()) {
            res.add(intervals.get(i));
            i++;
        }
        
        return res;
    }
}


 

LeetCode(57) Insert Interval

题目Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if neces...
  • fly_yr
  • fly_yr
  • 2015年09月14日 19:24
  • 993

LeetCode之Insert Interval--二分查找

题目描述 Given a set of non-overlapping intervals, insert a new interval into the intervals (merge ...

leetcode第一刷_Insert Interval

这道题的难度跟微软的那道面试题相当。 要在集合中插入一段新的集合,相当于求两个集合的并了。对于新加入一段集合的情况,分为下面几种: 1. 加入段跟原来的完全相交,也即他的起点和终点都在被包含在原来...

【leetcode】Array——Insert Interval(57)

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

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

Leetcode-Array-57(insert Interval)

当然,leetcode拥有良好的函数处理平台,但有时候我想知道如何编写出来一个完整的程序,现在我给出一种编写方法吧,请各位大神多多指教...

leetcode题解-57. Insert Interval

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

lkeetcode 57. Insert Interval

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

《leetCode》:Insert Interval

题目Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if neces...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Insert Interval
举报原因:
原因补充:

(最多只允许输入30个字)