Leetcode: insert Interval

Source: Internet
Author: User
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].
 1 /** 2  * Definition for an interval. 3  * public class Interval { 4  *     int start; 5  *     int end; 6  *     Interval() { start = 0; end = 0; } 7  *     Interval(int s, int e) { start = s; end = e; } 8  * } 9  */10 public class Solution {11     public List<Interval> insert(List<Interval> intervals, Interval newInterval) {12         if (newInterval == null) return intervals;13         if (intervals == null || intervals.size() == 0) {14             intervals.add(newInterval);15             return intervals;16         }17         int newleft = newInterval.start;18         int newright = newInterval.end;19         if (newleft > newright) return intervals;20         ArrayList<Interval> res = new ArrayList<Interval>();21         int lastleft = -1;22         int lastright = -1;23         int i = 0;24         for (; i<intervals.size(); i++) {25             Interval cur = intervals.get(i);26             if (newright < cur.start) {27                 if (lastleft == -1 && lastright == -1) {28                     lastleft = newleft;29                     lastright = newright;30                 }31                 else {32                     lastright = newright;33                 }34                 res.add(new Interval(lastleft, lastright));35                 res.add(cur);36                 break;37             }38             if (newleft < cur.start && newright >= cur.start && newright <= cur.end) {39                 if (lastleft == -1 && lastright == -1) {40                     lastleft = newleft;41                 }42                 lastright = cur.end;43                 res.add(new Interval(lastleft, lastright));44                 break;45             }46             if (newleft < cur.start && newright > cur.end) {47                 if (lastleft == -1 && lastright == -1) {48                     lastleft = newleft;49                 }50             }51             if (newleft >= cur.start && newright <= cur.end) {52                 res.add(cur);53                 break;54             }55             if (newleft >= cur.start && newleft <= cur.end && newright > cur.end) {56                 lastleft = cur.start;57             }58             if (newleft > cur.end) {59                 res.add(cur);60             }61         }62         for (i=i+1; i<intervals.size(); i++) {63             res.add(intervals.get(i));64         }65         if (newright > intervals.get(intervals.size()-1).end) {66             if (lastleft == -1 && lastright == -1) {67                 lastleft = newleft;68             }69             lastright = newright;70             res.add(new Interval(lastleft, lastright));71         }72         return res;73     }74 }

A simple practice of others:

 1 public ArrayList<Interval> insert(ArrayList<Interval> intervals, Interval newInterval) { 2     ArrayList<Interval> res = new ArrayList<Interval>(); 3     if(intervals.size()==0) 4     { 5         res.add(newInterval); 6         return res; 7     } 8     int i=0; 9     while(i<intervals.size() && intervals.get(i).end<newInterval.start)10     {11         res.add(intervals.get(i));12         i++;13     }14     if(i<intervals.size())15         newInterval.start = Math.min(newInterval.start, intervals.get(i).start);16     res.add(newInterval);17     while(i<intervals.size() && intervals.get(i).start<=newInterval.end)18     {19         newInterval.end = Math.max(newInterval.end, intervals.get(i).end);20         i++;21     }22     while(i<intervals.size())23     {24         res.add(intervals.get(i));25         i++;26     }27     return res;28 }

 

Leetcode: insert Interval

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.