Summary Ranges
You get a sorted array of distinct whole numbers. Cover them with the fewest ranges of consecutive numbers and write each range as "a->b", or as "a" if it holds one number. Our example: [2, 3, 4, 6, 8, 9, 11] — the answer is ["2->4", "6", "8->9", "11"].
- input
nums: different integers in ascending order; the array may be empty.- returns
- The fewest ranges that cover all of
numsand nothing else, in ascending order:"a->b"for the numbersatob,"a"foraalone. - constraints
- 0 ≤ nums.length ≤ 20
- −2³¹ ≤ nums[i] ≤ 2³¹ − 1
- nums: different, in ascending order
- examples
- Open a test in the tests panel: it shows the input and the expected answer.
/**
* @param {number[]} nums
* @return {string[]}
*/
function summaryRanges(nums) {
}
Ctrl ↵
format
reset
saved in this browser
Stuck?
See the solution run step by step on our example, then why it works and its complexity.
Next: more intervals problems
The problem and its statement belong to LeetCode. Here are our own retelling, example and solution. Patterns Hero is not affiliated with LeetCode.