Summary Ranges

Easy#228merge

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 nums and nothing else, in ascending order: "a->b" for the numbers a to b, "a" for a alone.
constraints
  • 0 ≤ nums.length ≤ 20
  • −2³¹ ≤ nums[i] ≤ 2³¹ − 1
  • nums: different, in ascending order
our 11 tests keep to them
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

11 tests · limit 3 s

Stuck?

See the solution run step by step on our example, then why it works and its complexity.

Watch the walkthrough →

Next: more intervals problems

Pattern “Intervals” →

The problem and its statement belong to LeetCode. Here are our own retelling, example and solution. Patterns Hero is not affiliated with LeetCode.