Skip to main content
Back to problems
Leetcode
Medium
Arrays
Intervals
Summary Ranges

Compress a sorted list of integers into compact range strings.

Acceptance 0%
Problem Statement

You are given a sorted array of integers with no duplicates. Group consecutive values into ranges and represent each range as a string.

  • A single number should be written as just that number.
  • A consecutive run of length at least 2 should be written as start->end.

Return the list of range strings in the same order as the numbers appear.

Input Format

  • An integer array nums.
  • nums is sorted in strictly increasing order.

Output Format

  • Return an array of strings describing the consecutive ranges in nums.

Constraints

  • 0nums.length0 \le nums.length
  • nums is sorted in strictly increasing order.
  • Adjacent elements may differ by 1 or more.
  • Use 32-bit signed integers for values.
Examples
Sample cases returned by the problem API.

Example 1

Input

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

Output

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

Explanation

The consecutive groups are [0,1,2], [4,5], and [7].

Example 2

Input

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

Output

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

Explanation

Only adjacent numbers are merged into a range.

Show 1 more example

Example 3

Input

nums = []

Output

[]

Explanation

An empty array produces no ranges.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.