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

LeetCode Insert Interval

时间:2015-10-16 23:23:53      阅读:392      评论:0      收藏:0      [点我收藏+]

标签:

原题链接在这里:https://leetcode.com/problems/insert-interval/

 

AC Java:

 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        List<Interval> res = new ArrayList<Interval>();
13        for(Interval curInterval : intervals){
14            if(curInterval.end < newInterval.start){
15                res.add(curInterval);
16            }else if(curInterval.start > newInterval.end){
17                res.add(newInterval);
18                newInterval = curInterval;
19            }else{
20                int newStart = Math.min(curInterval.start, newInterval.start);
21                int newEnd = Math.max(curInterval.end,newInterval.end);
22                Interval merged = new Interval(newStart,newEnd);
23                newInterval = merged;
24            }
25        }
26        res.add(newInterval);
27        return res;
28     }
29 }

 

[[1,5]] [0,0]

LeetCode Insert Interval

标签:

原文地址:http://www.cnblogs.com/Dylan-Java-NYC/p/4886624.html

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