-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path056.py
More file actions
24 lines (23 loc) · 714 Bytes
/
056.py
File metadata and controls
24 lines (23 loc) · 714 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# Definition for an interval.
# class Interval(object):
# def __init__(self, s=0, e=0):
# self.start = s
# self.end = e
class Solution(object):
def merge(self, intervals):
"""
:type intervals: List[Interval]
:rtype: List[Interval]
"""
if not intervals:
return []
inters = sorted(intervals, key=lambda x:(x.start, x.end))
# print(inters)
ans = [inters[0]]
for i in range(1, len(inters)):
if inters[i].start <= ans[-1].end:
if ans[-1].end < inters[i].end:
ans[-1].end = inters[i].end
else:
ans.append(inters[i])
return ans