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
本文解析了LeetCode中编号为57的难题,并提供了一种高效的解决方案。该题要求在一系列区间中插入一个新的区间,并返回更新后的区间列表。作者通过三种情况的讨论简化了解决方案。
1万+

被折叠的 条评论
为什么被折叠?



