[Leetcode] Insert Interval

Source: Internet
Author: User

Given a setNon-overlappingIntervals, 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 [], [], insert and merge [] in as [], [].

Example 2:
Given [], [], [], [], [], [], insert and merge [] in as [], [], [12, 16].

This is because the new interval [] overlaps with [], [], [].

Https://oj.leetcode.com/problems/insert-interval/

Idea 1: Create a New Result to return and traverse the original line segment group (note that it was originally sorted by START). Therefore, check whether the new interval overlaps with each other, add new results (the results must also be sorted by START ).

 

public class Solution {    public ArrayList<Interval> insert(ArrayList<Interval> intervals, Interval newInterval) {        if (newInterval == null)            return intervals;        ArrayList<Interval> res = new ArrayList<Interval>();        for (Interval each : intervals) {            if (each.end < newInterval.start)                res.add(each);            else if (each.start > newInterval.end) {                res.add(newInterval);                newInterval = each;            } else {                newInterval = new Interval(Math.min(each.start, newInterval.start), Math.max(each.end, newInterval.end));            }        }        res.add(newInterval);        return res;    }    public static void main(String[] args) {        // Given [1,2],[3,5],[6,7],[8,10],[12,16], insert and merge [4,9] in as        Interval one = new Interval(1, 2);        Interval two = new Interval(3, 5);        Interval three = new Interval(6, 7);        Interval four = new Interval(8, 10);        Interval five = new Interval(12, 16);        ArrayList<Interval> intervals = new ArrayList<Interval>();        intervals.add(one);        intervals.add(two);        intervals.add(three);        intervals.add(four);        intervals.add(five);        System.out.println(new Solution().insert(intervals, new Interval(4, 9)));    }}

 

 

 

Refer:

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

Http://www.cnblogs.com/TenosDoIt/p/3715013.html

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.