注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号
x
我的代码如下:
class Solution:
def summaryRanges(self, nums: List[int]) -> List[str]:
start, cur, end = nums[0], nums[0], None
output = []
for n in nums[1:]:
cur += 1
if n == cur:
end = n
else:
if not end:
output.append(str(start))
else:
output.append(str(start)+"->"+str(end))
start = n
cur = n
end = None
if not end:
output.append(str(start))
else:
output.append(str(start)+"->"+str(end))
return output
能run code 但是submit会报错:
IndexError: list index out of range
start, cur, end = nums[0], nums[0], None
Line 3 in summaryRanges (Solution.py)
ret = Solution().summaryRanges(param_1)
Line 42 in _driver (Solution.py)
_driver()
Line 53 in <module> (Solution.py) |