In this Leetcode Summary Ranges problem solution, You are given a sorted unique integer array nums.

Return the smallest sorted list of ranges that cover all the numbers in the array exactly. That is, each element of nums is covered by exactly one of the ranges, and there is no integer x such that x is in one of the ranges but not in nums.

Each range [a,b] in the list should be output as:

  1. "a->b" if a != b
  2. "a" if a == b

Leetcode Summary Ranges problem solution


Problem solution in Python.

class Solution(object):
def summaryRanges(self, nums):

    summary = []
    
    
    if len(nums) > 1:

        for i in range(len(nums)-1):

            if i == 0:
                start = nums[0]
                end = nums[0]

            if nums[i+1] == end+1:
                end = end+1

            else:
                if start!=end:
                    summary.append(str(start)+"->"+str(end))
                else:
                    summary.append(str(start))
                start = nums[i+1]
                end = nums[i+1]


        if start!=end:
            summary.append(str(start)+"->"+str(end))
        else:
            summary.append(str(start))
            
    
    if len(nums) == 1:
        summary.append(str(nums[0]))
    
    return summary



Problem solution in Java.

import java.util.*;

class Solution {
    public List<String> summaryRanges(int[] nums) {
        List<String> ranges = new ArrayList<>();
        int startI = 0;
        int endI = 0;
        for(int i=0; i<nums.length; i++) {
            startI = i;
            while(i < nums.length-1 && nums[i+1] == nums[i] + 1) {
                i++;
            }
            endI = i;
            
            if (startI == endI) {
                ranges.add(String.valueOf(nums[startI]));
            } else {
                ranges.add(nums[startI] + "->" + nums[endI]);
            }
        }
        return ranges;
    }
}


Problem solution in C++.

class Solution {
public:
    vector<string> summaryRanges(vector<int>& nums) {
        vector<string> result;
        for (int i = 0; i < nums.size(); ++i) {
            for (int j = i; j < nums.size(); ++j) {
                if (j == nums.size() - 1 || (long long)nums[j + 1] - nums[j] > 1) {
                    string s = to_string(nums[i]);
                    if (j > i) {
                        s += "->" + to_string(nums[j]);
                    }
                    result.push_back(s);
                    i = j;
                    break;
                }
            }
        }
        return result;
    }
};


Problem solution in C.

char ** summaryRanges(int* nums, int numsSize, int* returnSize){
    uint i,start,capacity = 8;
    char **output;
    char *str;
    *returnSize = 0;
    if(!numsSize)
        return output;
    output = (char**)malloc(sizeof(char*)*capacity);
    for(i = 0,start = 0; i < numsSize; i++)
    {
        if(nums[i] - (i - start) != nums[start])
        {
            str = (char*)malloc(sizeof(char)*25);
            if(i-start == 1)
                sprintf(str,"%d",nums[start]);
            else
                sprintf(str,"%d->%d",nums[start],nums[i-1]);
            if(*returnSize == capacity)
            {
                capacity *=2;
                output = (char**)realloc(output,sizeof(char*)*capacity);
            }
            output[(*returnSize)++] = str;
            start = i;
        }
    }
    str = (char*)malloc(sizeof(char)*23);
    if(i-start == 1)
        sprintf(str,"%d",nums[start]);
    else
        sprintf(str,"%d->%d",nums[start],nums[i-1]);
    if(*returnSize == capacity)
    {
        capacity += 1;
        output = (char**)realloc(output,sizeof(char*)*capacity);
    }
    output[(*returnSize)++] = str;
    start = i;
    return output;
}