标签:
原题链接在这里: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]
标签:
原文地址:http://www.cnblogs.com/Dylan-Java-NYC/p/4886624.html