-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy path641.missing-ranges.py
58 lines (52 loc) · 1.37 KB
/
641.missing-ranges.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# Tag: Simulation
# Time: O(N)
# Space: O(1)
# Ref: Leetcode-163
# Note: -
# Given a sorted integer array where **the range of elements are in the inclusive range [lower, upper]**, return its missing ranges.
#
# **Example 1**
# ```
# Input:
# nums = [0, 1, 3, 50, 75], lower = 0 and upper = 99
# Output:
# ["2", "4->49", "51->74", "76->99"]
# Explanation:
# in range[0,99],the missing range includes:range[2,2],range[4,49],range[51,74] and range[76,99]
# ```
# **Example 2**
# ```
# Input:
# nums = [0, 1, 2, 3, 7], lower = 0 and upper = 7
# Output:
# ["4->6"]
# Explanation:
# in range[0,7],the missing range include range[4,6]
# ```
#
#
from typing import (
List,
)
class Solution:
"""
@param nums: a sorted integer array
@param lower: An integer
@param upper: An integer
@return: a list of its missing ranges
"""
def find_missing_ranges(self, nums: List[int], lower: int, upper: int) -> List[str]:
# write your code here
res = []
for x in nums:
if lower < x:
res.append(self.output(lower, x - 1))
lower = x + 1
if lower <= upper:
res.append(self.output(lower, upper))
return res
def output(self, low: int, high: int) -> str:
if low == high:
return f'{low}'
else:
return f'{low}->{high}'