Summary Ranges
Easy · intervals
You are given a list of whole numbers nums, sorted from smallest to largest, with no repeats. Describe it as a list of ranges, where a range is a run of numbers that each are exactly 1 more than the one before.
Return one string per range, in increasing order:
- a run from
aup tob(two or more numbers) is written"a->b"; - a number with no neighbour that is 1 smaller or 1 larger is written
"a".
Every number belongs to exactly one range. If nums is empty, return an empty list.
Examples
Example 1
Input: nums = [0, 1, 2, 4, 5, 7]
Output: ["0->2", "4->5", "7"]
0, 1, 2 follow each other, then 4, 5, and 7 stands alone.
Example 2
Input: nums = [-3, -2, -1, 0, 1]
Output: ["-3->1"]
Every number is 1 more than the previous one, so the whole list is a single range.
Example 3
Input: nums = [2, 4, 6]
Output: ["2", "4", "6"]
Constraints
- 0 <= len(nums) <= 10^4
- -10^9 <= nums[i] <= 10^9
- nums is sorted in increasing order and has no repeated values