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

Note

You can test your code by connecting to Salesforce, but to save your progress and earn points, you'll need to create an account. Your solutions and achievements will be tracked automatically once you're logged in.

Wally can't hear you

Please sign in to access the AI Assistant

Sign In