#7 Summary Ranges

Companies:
T-Bank
Themes:
AlgorithmsArraysStringsPractice

A sorted array of unique integers nums is given:

class Solution {
    public List<String> summaryRanges(int[] nums) {
        ...
    }
}

A range [a,b] represents the set of all integers from a to b, inclusive. Return the smallest sorted list of ranges that covers all numbers in the array. Each range [a,b] in the list must be displayed as follows:

  • a -> b, if a != b.
  • a, if a == b.

Example 1:

Input: nums = [0, 1, 2, 4, 5, 7]
Output: ["0 -> 2","4 -> 5","7"]

Example 2:

Input: nums = [0, 2, 3, 4, 6, 8, 9]
Output: ["0","2 -> 4","6","8 -> 9"]

Question review

Mentor
Answer as you would in a real interview. I’ll ask follow-up questions, then compare your answer with the reference answer and show your accuracy as a %.
Sign in to start the dialogue The mentor keeps the context of the current question and your previous messages.
Sign in