class Solution: # @param intervals, a list of Intervals # @param newInterval, a Interval # @return a list of Interval def insert(self, intervals, newInterval): ans, inserted = [], False for i in range(len(intervals)): if intervals[i].end < newInterval.start: ans.append(intervals[i]) elif intervals[i].start > newInterval.end: if not inserted: inserted = True ans.append(newInterval) ans.append(intervals[i]) else: newInterval.start = min(newInterval.start, intervals[i].start) newInterval.end = max(newInterval.end, intervals[i].end) if len(ans) == 0 or newInterval.start > ans[-1].end: ans.append(newInterval) return ans
Thursday, September 18, 2014
Leetcode: Insert Interval @Python
Subscribe to:
Post Comments
(
Atom
)
No comments :
Post a Comment