1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | public class Solution { public List<Interval> insert(List<Interval> intervals, Interval newInterval) { List<Interval> result = new ArrayList<Interval>(); for (Interval interval:intervals){ if (interval.end < newInterval.start){ result.add(interval); }else if (interval.start > newInterval.end){ result.add(newInterval); newInterval = interval; }else{ newInterval = new Interval(Math.min(interval.start, newInterval.start), Math.max(interval.end, newInterval.end)); } } result.add(newInterval); return result; } } |
Wednesday, November 25, 2015
[leetcode]Insert Interval
方法就是跟interval没交集的时候 要是在前就push 进去 在后就hold着它,把interval push进去..然后等下一次(肯定会在下一个的前面 input的assumption) 直到loop完再补一个.. 还有merge的话就只merge 然后hold住不push因为有可能不只merge 一个
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment