LeetCode #163. Missing Ranges
作者:互联网
题目
解题方法
设置start = lower,遍历nums,如果当前位置的数字nums[i]不等于start,就把nums[i]-1作为end,调用self.fun(start,end),把区间加进去。每次循环最后设置start = nums[i] + 1,最后一次循环结束后如果start != upper + 1,则需要将最后一个区间加进去。
时间复杂度:O(n)
空间复杂度:O(n)
代码
class Solution:
def fun(self, start, end):
if start == end:
return str(start)
else:
return str(start) + "->" + str(end)
def findMissingRanges(self, nums: List[int], lower: int, upper: int) -> List[str]:
start = lower
rat = []
for i in range(len(nums)):
if nums[i] != start:
rat.append(self.fun(start, nums[i] - 1))
start = nums[i] + 1
else:
if start != upper + 1:
rat.append(self.fun(start, upper))
return rat
标签:upper,end,nums,self,Missing,Ranges,start,rat,LeetCode 来源: https://www.cnblogs.com/RatsCommander/p/14062286.html