-
Notifications
You must be signed in to change notification settings - Fork 43
/
summary-ranges.py
197 lines (169 loc) · 5.25 KB
/
summary-ranges.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
"""
228. Summary Ranges
Easy
You are given a sorted unique integer array nums.
A range [a,b] is the set of all integers from a to b (inclusive).
Return the smallest sorted list of ranges that cover all the numbers in the array exactly. That is, each element of nums is covered by exactly one of the ranges, and there is no integer x such that x is in one of the ranges but not in nums.
Each range [a,b] in the list should be output as:
"a->b" if a != b
"a" if a == b
Example 1:
Input: nums = [0,1,2,4,5,7]
Output: ["0->2","4->5","7"]
Explanation: The ranges are:
[0,2] --> "0->2"
[4,5] --> "4->5"
[7,7] --> "7"
Example 2:
Input: nums = [0,2,3,4,6,8,9]
Output: ["0","2->4","6","8->9"]
Explanation: The ranges are:
[0,0] --> "0"
[2,4] --> "2->4"
[6,6] --> "6"
[8,9] --> "8->9"
Constraints:
0 <= nums.length <= 20
-231 <= nums[i] <= 231 - 1
All the values of nums are unique.
nums is sorted in ascending order.
"""
# V0
class Solution:
def summaryRanges(self, nums):
if not nums:
return []
# deal with edge case
nums = nums + [nums[-1]+2]
res = []
# set head = nums[0]
head = nums[0]
# start from index = 1
for i in range(1,len(nums)):
if nums[i] - nums[i-1] > 1:
if head == nums[i-1]:
res.append(str(head))
else:
res.append(str(head) + "->" + str(nums[i-1]))
head = nums[i]
return res
# V0'
class Solution:
def summaryRanges(self, nums):
start, end, res = -2**31, -2**31, []
for i in nums+[2**31-1]:
if i - end == 1:
end = i
else:
res += [str(start)] if start == end else [str(start) + "->" + str(end)]
start = end = i
return res[1:]
# V1
# http://bookshadow.com/weblog/2015/06/26/leetcode-summary-ranges/
class Solution:
# @param {integer[]} nums
# @return {string[]}
def summaryRanges(self, nums):
x, size = 0, len(nums)
ans = []
while x < size:
c, r = x, str(nums[x])
while x + 1 < size and nums[x + 1] - nums[x] == 1:
x += 1
if x > c:
r += "->" + str(nums[x])
ans.append(r)
x += 1
return ans
### Test case : dev
# V1'
# https://leetcode.com/problems/summary-ranges/discuss/63335/Simple-Python-solution
class Solution:
def summaryRanges(self, nums):
if not nums:
return []
nums = nums + [nums[-1]+2]
res = []
head = nums[0]
for i in range(1,len(nums)):
if nums[i] - nums[i-1] > 1:
if head == nums[i-1]:
res.append(str(head))
else:
res.append(str(head) + "->" + str(nums[i-1]))
head = nums[i]
return res
# V1''
# https://leetcode.com/problems/summary-ranges/discuss/63335/Simple-Python-solution
class Solution:
def summaryRanges(self, nums):
start, end, res = -2**31, -2**31, []
for i in nums+[2**31-1]:
if i - end == 1:
end = i
else:
res += [str(start)] if start == end else [str(start) + "->" + str(end)]
start = end = i
return res[1:]
# V1'
# http://bookshadow.com/weblog/2015/06/26/leetcode-summary-ranges/
# IDEA : COLLECT THE RANGES, FORMTAT AND RETURN THEM
def summaryRanges(self, nums):
ranges = []
for n in nums:
if not ranges or n > ranges[-1][-1] + 1:
ranges += [],
ranges[-1][1:] = n,
return ['->'.join(map(str, r)) for r in ranges]
# V1''
# http://bookshadow.com/weblog/2015/06/26/leetcode-summary-ranges/
# IDEA : A variation of solution 1', holding the current range in an extra variable r to make things easier.
def summaryRanges(self, nums):
ranges = []
for n in nums:
if not ranges or n > ranges[-1][-1] + 1:
ranges += [],
ranges[-1][1:] = n,
return ['->'.join(map(str, r)) for r in ranges]
# V1'''
# http://bookshadow.com/weblog/2015/06/26/leetcode-summary-ranges/
def summaryRanges(self, nums):
ranges = r = []
for n in nums:
if `n-1` not in r:
r = []
ranges += r,
r[1:] = `n`,
return map('->'.join, ranges)
# V2
# Time: O(n)
# Space: O(1)
import itertools
import re
class Solution(object):
# @param {integer[]} nums
# @return {string[]}
def summaryRanges(self, nums):
ranges = []
if not nums:
return ranges
start, end = nums[0], nums[0]
for i in range(1, len(nums) + 1):
if i < len(nums) and nums[i] == end + 1:
end = nums[i]
else:
interval = str(start)
if start != end:
interval += "->" + str(end)
ranges.append(interval)
if i < len(nums):
start = end = nums[i]
return ranges
# Time: O(n)
# Space: O(n)
class Solution2(object):
# @param {integer[]} nums
# @return {string[]}
def summaryRanges(self, nums):
return [re.sub('->.*>', '->', '->'.join(repr(n) for _, n in g))
for _, g in itertools.groupby(enumerate(nums), lambda i_n: i_n[1]-i_n[0])]