-
Notifications
You must be signed in to change notification settings - Fork 481
/
0228.py
30 lines (28 loc) · 845 Bytes
/
0228.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
class Solution:
def summaryRanges(self, nums):
"""
:type nums: List[int]
:rtype: List[str]
"""
nums_len = len(nums)
if nums_len == 0:
return []
result = list()
start, end, pre, cur = 0, 0, 0, 1
while cur <= nums_len:
if cur < nums_len and nums[cur] - nums[pre] == 1:
cur += 1
pre += 1
else:
end = pre
if end != start:
result.append(str(nums[start])+"->"+str(nums[end]))
else:
result.append(str(nums[start]))
start = cur
cur += 1
pre += 1
return result
if __name__ == "__main__":
nums = [0,2,3,4,6,8,9]
print(Solution().summaryRanges(nums))