-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsummaryRanges.py
70 lines (60 loc) · 1.75 KB
/
summaryRanges.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
class Solution(object):
def summaryRanges(self, nums):
"""
:type nums: List[int]
:rtype: List[str]
"""
if len(nums)==0:
return nums
start = nums[0]
start_str = str(start)
out = [start_str]
prev = start
for a in (nums[1:]):
if a-prev==1:
out[-1]=start_str+'->'+str(a)
else:
start = a
start_str = str(start)
out.append(start_str)
prev = a
return out
def summaryRangeDuplicates(self,nums):
#duplicates
if len(nums) == 0:
return nums
prev = start = nums[0]
startstr = str(start)
out = [startstr]
for a in nums[1:]:
if a-prev == 1 or a==prev:
out[-1]=startstr+'->'+str(a)
else:
start = a
startstr = str(start)
out.append(startstr)
prev = a
return out
def summaryRangesVaried(self,nums):
#varied ranges
if len(nums) == 0:
return nums
prev = start = nums[0]
startstr =str(start)
out = [startstr]
diff = nums[1]-start
diffstr = str(diff)
len_ = len(nums)
for i in xrange(1,len_):
n = nums[i]
tmp = n - prev
if tmp == diff:
out[-1] = startstr +'->' + str(n)+'/'+ diffstr
else:
diff = nums[min(i+1,len_-1)]-n
diffstr = str(diff)
start = n
startstr = str(start)
out.append(startstr)
prev = n
return out