Skip to content

Practice Summary Ranges

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Easy#228
LeetCode Problem

Summary Ranges

Problem Statement

Given the sorted array nums of distinct integers, group consecutive values into the fewest possible inclusive ranges. The ranges must be sorted and cover exactly the input values, with no gaps included and no value covered twice. Represent a range from a to b as "a->b" when a and b differ; represent a single value as "a". Return the list of these strings.

Source: Summary Ranges on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

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

# Example 2

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

Constraints

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

Related LeetCode Problems

Practice more problems similar to Summary Ranges.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.