forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.py
32 lines (32 loc) · 935 Bytes
/
Solution.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
class Solution(object):
def summaryRanges(self, nums):
"""
:type nums: List[int]
:rtype: List[str]
"""
length = len(nums)
if length == 0:
return []
ans = []
count = 0
idx = 0
while idx + count < length - 1:
if nums[idx + count] == nums[idx + count + 1] - 1:
count += 1
else:
string = ''
if count == 0:
string = str(nums[idx])
else:
string = str(nums[idx]) + "->" + str(nums[idx + count])
ans.append(string)
idx += count + 1
count = 0
# 末尾处理
string = ''
if count > 0:
string = str(nums[idx]) + "->" + str(nums[idx + count])
else:
string = str(nums[idx])
ans.append(string)
return ans