Summary Ranges
Given a sorted unique integer array nums, return the smallest sorted list of ranges that covers all the numbers exactly. Each range [a,b] is formatted as 'a->b' if a != b, or just 'a' if it is a single number.
Open official problem prompt ↗Compress a sorted list of distinct integers into the fewest contiguous ranges that together contain exactly those integers.
Like listing page numbers you read: instead of '3,4,5,6' you write '3-6', and a lone page just as itself.
- Input
- nums = [0,1,2,4,5,7]
- Output
- ["0->2","4->5","7"]
- Why
- 0,1,2 are consecutive so they form 0->2; 4,5 form 4->5; 7 stands alone.
0 <= nums.length <= 20-2^31 <= nums[i] <= 2^31 - 1All values are uniquenums is sorted in ascending order