码迷,mamicode.com
首页 > 其他好文 > 详细

Leetcode: Insert Interval

时间:2014-10-05 13:53:58      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   ar   for   sp   div   art   

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 }

别人的一个比较简便的做法:

 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

标签:style   blog   color   io   ar   for   sp   div   art   

原文地址:http://www.cnblogs.com/EdwardLiu/p/4006905.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!