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

题解:

这道题不仅要insert newInterval同时还要保证能够merge。那么就分情况讨论。

遍历每一个已给出的interval,

当当前的interval的end小于newInterval的start时,说明新的区间在当前遍历到的区间的后面,并且没有重叠,所以res添加当前的interval;

当当前的interval的start大于newInterval的end时,说明新的区间比当前遍历到的区间要前面,并且也没有重叠,所以把newInterval添加到res里,并更新newInterval为当前的interval;

当当前的interval与newInterval有重叠时,merge interval并更新新的newInterval为merge后的。

代码如下:

1     public ArrayList<Interval> insert(ArrayList<Interval> intervals, Interval newInterval) {
 2         ArrayList<Interval> res = new ArrayList<Interval>();
 3             
 4         for(Interval each: intervals){
 5             if(each.end < newInterval.start){
 6                 res.add(each);
 7             }else if(each.start > newInterval.end){
 8                 res.add(newInterval);
 9                 newInterval = each;        
10             }else if(each.end >= newInterval.start || each.start <= newInterval.end){
11                 int nstart = Math.min(each.start, newInterval.start);
12                 int nend = Math.max(newInterval.end, each.end);
13                 newInterval = new Interval(nstart, nend);
14             }
15         }
16  
17         res.add(newInterval); 
18  
19         return res;
20     }

Reference://http://www.programcreek.com/2012/12/leetcode-insert-interval/

Insert Interval leetcode java

时间: 2024-11-12 13:22:10

Insert Interval leetcode java的相关文章

Merge Interval leetcode java

题目: Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6],[8,10],[15,18], return [1,6],[8,10],[15,18]. 题解: 这道题主要难点是改写Comparator. Comparator接口定义了两个方法:compare( )和equals( ).这里给出的compare( )方法按顺序比较了两个元素: int comp

Search Insert Position leetcode java

Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You may assume no duplicates in the array. Here are few examples.        [1,3,5,6], 5 → 2      

Insert Interval -- leetcode

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

2015.04.01 Leetcode Insert interval

Insert Interval解法: 先用start,end两个变量来定位 newinterval可能要插入的起始点和结束点. 分三种情况来看是否需要调整或合并(merge). case1: 在start 之前直接copy 到结果res: case2: 在end之后也是直接copy到res中; case3: 需要合并的就是newInterval.start, newInterval.end, 和任选的一个interval 的interval.start , interval.end 四个边界的比

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: Given intervals [1,3],[6,9], insert and merge

【leetcode刷题笔记】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 [

[leetcode]Insert Interval @ Python

原题地址:https://oj.leetcode.com/problems/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

[LeetCode][JavaScript]Insert Interval

https://leetcode.com/problems/insert-interval/ 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 t

LeetCode: Insert Interval [056]

[题目] 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 m