Leetcode question #228
Question:
Given a sorted integer array without duplicates, return the summary of its ranges.
For example, given
[0,1,2,4,5,7]
, return ["0->2","4->5","7"].
class Solution(object):
def summaryRanges(self, nums):
"“”
:type nums: List[int]
:rtype: List[str]
"“”
flag1 = 0
flag2 = 0
res = []
if len(nums) ==0:
return []
elif len(nums) ==1:
return [str(nums[flag1])]
else:
for flag2 in range(0,len(nums)):
flag2+=1
if (flag2 < len(nums) and nums[flag2]>nums[flag2-1]+1):
if (nums[flag1] != nums[flag2-1]):
res.append(str(nums[flag1])+“->”+str(nums[flag2-1]))
else:
res.append(str(nums[flag1]))
flag1 = flag2
if flag2 == flag1 + 1 and flag1== len(nums)-1:
res.append(str(nums[flag1]))
if flag2 == len(nums) and flag1 < len(nums) and nums[flag2-1] != nums[flag1]:
res.append(str(nums[flag1])+“->”+str(nums[flag2-1]))
return res
No comments:
Post a Comment