bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 228. Summary Ranges

228. Summary Ranges

Leetcode Solutions

Key approach of the solution: Fix Left Bound

  1. Initialize an empty list ranges to store the result.
  2. Use a pointer i to iterate over the elements of nums.
  3. For each iteration, store the beginning of the current range in start = nums[i].
  4. Use a while loop to check if the next element nums[i + 1] is consecutive to nums[i]. If so, increment i by 1.
  5. Once a non-consecutive element is found or the end of the array is reached, check if start is equal to nums[i]. If true, add start as a string to ranges. Otherwise, add the string start->nums[i] to ranges.
  6. Increment i by 1 to start a new range.
  7. Repeat steps 3 to 6 until all elements have been covered.
  8. Return the ranges list.
UML Thumbnail

Alternative approach: Two Pointers

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...