Given a sorted list of unique integers, return the smallest sorted list of ranges that exactly covers all the numbers. Each range is formatted as 'a->b', or just 'a' when the range contains a single number (a == b). Return an empty list for null or empty input.
Examples:
Input: nums = [0, 1, 2, 4, 5, 7]
Output: ['0->2', '4->5', '7']
Explanation: 0,1,2 form a consecutive range; 4,5 form a consecutive range; 7 stands alone.
Input: nums = [0, 2, 3, 4, 6, 8, 9]
Output: ['0', '2->4', '6', '8->9']
Explanation: 0 stands alone; 2,3,4 form a consecutive range; 6 stands alone; 8,9 form a consecutive range.
Input: nums = [1]
Output: ['1']
Explanation: A single element forms a range of just itself.
Input: nums = []
Output: []
Explanation: Empty input returns an empty list.
Input: nums = null
Output: []
Explanation: Null input returns an empty list.
Apex Code Editor
Welcome to Lightning Challenge!
How It Works
- • Write your solution in the code editor
- • Connect your Salesforce org to test
- • Submit to check if your solution passes
- • Use hints if you get stuck
Contest Alert
🏆 All Time Leaderboard
Contest runs January 1, 1970 - December 31, 9999. Complete challenges to climb the leaderboard!
Points are calculated for challenges completed during the contest period.