Menu
CoddyTech

Summary Ranges

You get a sorted array nums of distinct integers. Split it into the fewest ranges of consecutive integers, so that every value belongs to exactly one range. Write a range a..b as the text "a->b", or as "a" when it holds one value. Return the ranges in increasing order.

Function

summaryRanges(nums: integer-array) → string-array
numsinteger-array
the sorted array of distinct integers
Returnsstring-array
the ranges as text, from the smallest values to the largest

Constraints

  • 1 ≤ nums.length ≤ 5000
  • -109 ≤ nums[i] ≤ 109
  • nums is sorted in increasing order and has no duplicates.

Examples

Input
nums = [0, 1, 2, 5, 6, 9]
Output
["0->2", "5->6", "9"]
Explanation
0, 1, 2 follow each other, so they form "0->2". The jump from 2 to 5 starts a new range, "5->6", and 9 stands alone as "9".

lock icon+16 hidden tests on Submit

challenge icon

Follow-up

Suppose nums could hold duplicates, like [1, 2, 2, 3]. What would you change so that it still prints "1->3"?

Reset code
def summaryRanges(nums):
    # Write code here
Test cases

Case 1

Case 2

Input

nums = [0, 1, 2, 5, 6, 9]

Expected

["0->2", "5->6", "9"]