Missing Ranges
EasyArray · Apple
You are given an inclusive range [lower, upper] and a sorted array nums of unique integers, every one of which lies inside that range. A number is *missing* if it is in [lower, upper] but does not appear in nums.
Return all missing numbers as a list of inclusive ranges [start, end]:
- Each range must be a maximal run of consecutive missing numbers, so two returned ranges never touch or overlap.
- A lone missing number
xis written as[x, x]. - Ranges are sorted in increasing order.
If no number is missing, return an empty list.
Example 1
- Input:
- nums = [3,4,8], lower = 1, upper = 10
- Output:
- [[1,2],[5,7],[9,10]]
- Explanation
- 1 and 2 are missing before 3, then 5 through 7 between 4 and 8, then 9 and 10 after 8. Each stretch becomes one range.
Example 2
- Input:
- nums = [], lower = -2, upper = 2
- Output:
- [[-2,2]]
- Explanation
- Nothing is present, so the whole interval is one missing range.
Example 3
- Input:
- nums = [5,6,7], lower = 5, upper = 7
- Output:
- []
- Explanation
- Every number from 5 to 7 is present, so nothing is missing.
Constraints
-10^9 <= lower <= upper <= 10^90 <= nums.length <= 100lower <= nums[i] <= uppernumsis sorted in increasing order and all its values are unique