-
Notifications
You must be signed in to change notification settings - Fork 0
/
letcode_56_合并区间.py
48 lines (39 loc) · 1.13 KB
/
letcode_56_合并区间.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# -*- encoding: utf-8 -*-
"""
@File : letcode_56_合并区间.py
@Time : 2020/8/23 下午8:32
@Author : dididididi
@Email :
@Software: PyCharm
"""
class Solution(object):
def merge(self, intervals):
"""
:type intervals: List[List[int]]
:rtype: List[List[int]]
"""
# 数组排序,逆向
intervals.sort()
merged = []
for interval in intervals:
if not merged or merged[-1][1] < interval[0]:
merged.append(interval)
else:
merged[-1][1] = max(merged[-1][1], interval[1])
return merged
# 数组排序,正向
intervals.sort()
res = []
i = 0
while i < len(intervals):
left = intervals[i][0]
right = intervals[i][1]
while i < len(intervals) - 1 and right >= intervals[i + 1][0]:
right = max(right, intervals[i + 1][1])
i = i + 1
res.append([left, right])
i = i + 1
return res
if __name__ == '__main__':
sol = Solution()
print(sol.merge([[8,10],[15,18],[2,14],[1,3]]))