Skip to content

Instantly share code, notes, and snippets.

@hoanbka
Created November 7, 2017 18:51
Show Gist options
  • Save hoanbka/8d3a5a4103533c29a057a805a16ab3c2 to your computer and use it in GitHub Desktop.
Save hoanbka/8d3a5a4103533c29a057a805a16ab3c2 to your computer and use it in GitHub Desktop.
# https://leetcode.com/problems/summary-ranges/description/
class Solution:
def summaryRanges(self, nums):
"""
:type nums: List[int]
:rtype: List[str]
"""
output = []
temp = ""
for i in range(len(nums)):
if len(temp) == 0:
temp += str(nums[i])
if i == len(nums) - 1:
output.append(temp)
temp = ""
else:
if nums[i] + 1 != nums[i + 1]:
output.append(temp)
temp = ""
else:
if i == len(nums) - 1:
temp += "->"
temp += str(nums[i])
output.append(temp)
temp = ""
else:
if nums[i] + 1 != nums[i + 1]:
temp += "->"
temp += str(nums[i])
output.append(temp)
temp = ""
if temp:
output.append(temp)
return output
s = Solution()
print(s.summaryRanges([0, 1, 2, 4, 5, 7]))
print(s.summaryRanges([0, 2, 3, 4, 6, 8, 9]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment