题解 | #合并区间#
合并区间
https://www.nowcoder.com/practice/69f4e5b7ad284a478777cb2a17fb5e6a
用栈的思路可以完美解决。题中定义的start 和end有点坑底,不过还好
# class Interval: # def __init__(self, a=0, b=0): # self.start = a # self.end = b # # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param intervals Interval类一维数组 # @return Interval类一维数组 # class Solution: def merge(self , intervals: List[int]) -> List[List[int]]: # write code here stack = [] intervals.sort(key=lambda x:(x.start)) for i in intervals: if not stack: stack.append(i) else: if stack[-1].end >= i.start: if stack[-1].end < i.end: stack[-1].end = i.end else: continue else: stack.append(i) return stack