My concise c++ solution with 0ms


  • 3
    B

    class Solution {
    public:

    vector<string> summaryRanges(vector<int>& nums) {
        
        vector<string> result;
        for(int i = 0; i < nums.size();)
        {
            int j = i + 1;
            while(j < nums.size() && ((j-i) == (nums[j]-nums[i]))) j++;  //(j-i) == (nums[j]-nums[i])
            
            if(j-1 > i)
            {
                result.push_back(to_string(nums[i]) + "->" + to_string(nums[j-1]));
            }
            else
            {
                result.push_back(to_string(nums[i]));
            }
            
            i = j; //Update i
        }
        
        return result;
    }
    

    };


  • 0
    J
    This post is deleted!

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.