-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path0163-missing-ranges.js
More file actions
35 lines (32 loc) · 988 Bytes
/
0163-missing-ranges.js
File metadata and controls
35 lines (32 loc) · 988 Bytes
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
/**
* 163. Missing Ranges
* https://leetcode.com/problems/missing-ranges/
* Difficulty: Easy
*
* 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.
*/
/**
* @param {number[]} nums
* @param {number} lower
* @param {number} upper
* @return {number[][]}
*/
var findMissingRanges = function(nums, lower, upper) {
const result = [];
let prev = lower - 1;
for (let i = 0; i <= nums.length; i++) {
const curr = i < nums.length ? nums[i] : upper + 1;
if (curr - prev > 1) {
result.push([prev + 1, curr - 1]);
}
prev = curr;
}
return result;
};