-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSummary Ranges.py
More file actions
35 lines (33 loc) · 931 Bytes
/
Copy pathSummary Ranges.py
File metadata and controls
35 lines (33 loc) · 931 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
25
26
27
28
29
30
31
32
33
34
35
__author__ = 'Martin'
class Solution(object):
def summaryRanges(self, nums):
"""
:type nums: List[int]
:rtype: List[str]
"""
if len(nums) == 0:
return []
strs = []
temp = []
last = nums[0]
temp.append(last)
for n in nums[1:len(nums)]:
if n == last + 1:
temp.append(n)
last = n
else:
if len(temp) == 1:
strs.append(str(temp[0]))
else:
strs.append(str(temp[0]) + "->" + str(temp[len(temp)-1]))
temp = []
temp.append(n)
last = n
if len(temp) == 1:
strs.append(str(temp[0]))
else:
strs.append(str(temp[0]) + "->" + str(temp[len(temp)-1]))
return strs
s = Solution()
nums = [1,2,4,5,7]
print(s.summaryRanges(nums))