You are given an inclusive range [lower, upper]
and a sorted unique integer array nums
, where all elements are within 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 shortest sorted list of ranges that exactly covers all the missing numbers. That is, no element of nums
is included in any of the ranges, and each missing number is covered by one of the ranges.
Example 1:
Input: nums = [0,1,3,50,75], lower = 0, upper = 99 Output: [[2,2],[4,49],[51,74],[76,99]] Explanation: The ranges are: [2,2] [4,49] [51,74] [76,99]
Example 2:
Input: nums = [-1], lower = -1, upper = -1 Output: [] Explanation: There are no missing ranges since there are no missing numbers.
Constraints:
-109 <= lower <= upper <= 109
0 <= nums.length <= 100
lower <= nums[i] <= upper
- All the values of
nums
are unique.
class Solution:
def findMissingRanges(self, nums: List[int], lower: int, upper: int) -> List[str]:
def f(a, b):
return str(a) if a == b else f'{a}->{b}'
n = len(nums)
if n == 0:
return [f(lower, upper)]
ans = []
if nums[0] > lower:
ans.append(f(lower, nums[0] - 1))
for a, b in pairwise(nums):
if b - a > 1:
ans.append(f(a + 1, b - 1))
if nums[-1] < upper:
ans.append(f(nums[-1] + 1, upper))
return ans
class Solution {
public List<String> findMissingRanges(int[] nums, int lower, int upper) {
int n = nums.length;
List<String> ans = new ArrayList<>();
if (n == 0) {
ans.add(f(lower, upper));
return ans;
}
if (nums[0] > lower) {
ans.add(f(lower, nums[0] - 1));
}
for (int i = 1; i < n; ++i) {
int a = nums[i - 1], b = nums[i];
if (b - a > 1) {
ans.add(f(a + 1, b - 1));
}
}
if (nums[n - 1] < upper) {
ans.add(f(nums[n - 1] + 1, upper));
}
return ans;
}
private String f(int a, int b) {
return a == b ? a + "" : a + "->" + b;
}
}
class Solution {
public:
vector<string> findMissingRanges(vector<int>& nums, int lower, int upper) {
auto f = [](int a, int b) {
return a == b ? to_string(a) : to_string(a) + "->" + to_string(b);
};
int n = nums.size();
vector<string> ans;
if (n == 0) {
ans.emplace_back(f(lower, upper));
return ans;
}
if (nums[0] > lower) {
ans.emplace_back(f(lower, nums[0] - 1));
}
for (int i = 1; i < n; ++i) {
int a = nums[i - 1], b = nums[i];
if (b - a > 1) {
ans.emplace_back(f(a + 1, b - 1));
}
}
if (nums[n - 1] < upper) {
ans.emplace_back(f(nums[n - 1] + 1, upper));
}
return ans;
}
};
func findMissingRanges(nums []int, lower int, upper int) (ans []string) {
f := func(a, b int) string {
if a == b {
return strconv.Itoa(a)
}
return strconv.Itoa(a) + "->" + strconv.Itoa(b)
}
n := len(nums)
if n == 0 {
ans = append(ans, f(lower, upper))
return
}
if nums[0] > lower {
ans = append(ans, f(lower, nums[0]-1))
}
for i := 1; i < n; i++ {
a, b := nums[i-1], nums[i]
if b-a > 1 {
ans = append(ans, f(a+1, b-1))
}
}
if nums[n-1] < upper {
ans = append(ans, f(nums[n-1]+1, upper))
}
return
}