Skip to content

Latest commit

 

History

History
27 lines (19 loc) · 653 Bytes

163. Missing Ranges.md

File metadata and controls

27 lines (19 loc) · 653 Bytes

163. Missing Ranges

Given a sorted integer array where the range of elements are [0, 99] inclusive, return its missing ranges. For example, given [0, 1, 3, 50, 75], return [“2”, “4->49”, “51->74”, “76->99”]

Example

For example, given [0, 1, 3, 50, 75], return [“2”, “4->49”, “51->74”, “76->99”]

Code

nums = [0, 1, 3, 50, 75]
output = []
nums.insert(0,-1)
nums.append(100)
length = len(nums)
for i in range(1,length):
    if nums[i]-nums[i-1]>2:
        output.append(str(nums[i-1]+1)+"->"+str(nums[i]-1))
    elif nums[i]-nums[i-1]==2:
        output.append(str(nums[i-1]+1))
print(output)