Leetcode 题目
再做一道 Array 的题目,#57
标了Hard,发现好像不难。只要分三种情况讨论就好了,一是比newinterval.start小,二是比newinterval.end大,三是其他。
class Solution:
def insert(self, intervals, newInterval):
ret = []
ok = 0
for i in intervals:
if i.end < newInterval.start:
ret.append(i)
elif i.start > newInterval.end:
if not ok:
ret.append(newInterval)
ok = 1
ret.append(i)
else:
newInterval.start = min(newInterval.start, i.start)
newInterval.end = max(newInterval.end, i.end)
if not ok:
ret.append(newInterval)
ok = 1
return ret