#228

Summary Ranges

pupil · 465 · lc easy +26 · verified · 54% accepted · 4,507 likes · top 46%

play →

Description

You are given a sorted array of unique integers nums.

A range [a,b] covers every integer from a to b inclusive.

Return the minimal sorted list of ranges that together cover exactly every element of nums. Each element belongs to exactly one range and no extra integers are included.

Format each range [a,b] as:

- "a->b" when a != b

- "a" when a == b

Example 1:

Input: nums = [0,1,2,4,5,7]
Output: ["0->2","4->5","7"]
Explanation: The ranges are:
[0,2] --> "0->2"
[4,5] --> "4->5"
[7,7] --> "7"

Example 2:

Input: nums = [0,2,3,4,6,8,9]
Output: ["0","2->4","6","8->9"]
Explanation: The ranges are:
[0,0] --> "0"
[2,4] --> "2->4"
[6,6] --> "6"
[8,9] --> "8->9"

Code

1
2
3