33 · Intervals

#228 Summary Ranges

Easysolve on LeetCode ↗

frequently reported atNetflixGoogleMeta

the problem

In plain words

nums is sorted in increasing order and has no duplicates. Describe it with the fewest ranges, in order: a run of consecutive numbers a, a+1, …, b becomes "a->b", and a number on its own becomes "a".

in nums = [0,1,2,4,5,7]

out ["0->2","4->5","7"]

0,1,2 is one run; 4,5 is another; 7 stands alone.

in nums = [0,2,3,4,6,8,9]

out ["0","2->4","6","8->9"]

  • 0 ≤ nums.length ≤ 20
  • −2³¹ ≤ nums[i] ≤ 2³¹ − 1
  • Sorted in increasing order, all values unique

your turn

Try it before you peek

Read the examples, say the brute force out loud, then write code: here or on LeetCode ↗. Stuck for 20 minutes? Open one hint at a time.

Solve it in the browser

LeetCode-style: complete the class. Compiled by the real javac (Java 17) and judged right here — nothing is sent anywhere.

    Solutions are hidden so you can try first. Even 20 minutes of real effort makes the answer stick far better than reading it.

    Marking the problem solved, or opening every hint, also shows it.

    practise next

    Similar problems

    1. #128
    2. #56
      Merge Intervals
      Mediumin Intervals