163. Missing Ranges
Problem description:
You are given an inclusive range [lower, upper]
and a sorted unique integer array nums
, where all elements are in the inclusive range.
A number x
is considered missing if x
is in the range [lower, upper]
and x
is not in nums
.
Return the smallest sorted list of ranges that cover every missing number exactly. That is, no element of nums
is in any of the ranges, and each missing number is in one of the ranges.
Each range [a,b]
in the list should be output as:
"a->b"
ifa != b
"a"
ifa == b
Example 1:
1 | Input: nums = [0,1,3,50,75], lower = 0, upper = 99 |
Solution:
- Find the range between
lower
and first element in the array. - Find ranges between adjacent elements in the array.
- Find the range between
upper
and last element in the array.
1 | class Solution: |
time complexity: $O(n)$
space complexity: $O(1)$
reference:
related problem: