Skip to content
Decoder

Summary Ranges

Easy

Problem

nums is a sorted list of unique integers. Describe it as the smallest list of ranges that covers exactly those numbers. Each run of consecutive integers from a to b becomes the string "a->b", and a number on its own becomes "a". Return the ranges in order.

Example 1

Input
[0, 1, 2, 4, 5, 7]
Output
["0->2", "4->5", "7"]

Example 2

Input
[0, 2, 3, 4, 6, 8, 9]
Output
["0", "2->4", "6", "8->9"]

Example 3

Input
[]
Output
[]

Constraints

  • 0 <= nums.length <= 20
  • -2^31 <= nums[i] <= 2^31 - 1
  • All values are unique and sorted in ascending order.

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.