9 lines, c++, 0ms solution


  • 13
    X
     vector<string> summaryRanges(vector<int>& nums) {
        int i = 0, size = nums.size();
        vector<string> result; 
        while(i < size){
            int j = 1; 
            while(i + j < size && nums[i + j] - nums[i] == j) ++j;
            result.push_back(j <= 1 ? to_string(nums[i]) : to_string(nums[i]) + "->" + to_string(nums[i + j - 1]));
            i += j; 
        }
        return result; 
    }

  • 0

    Very concise solution!


Log in to reply
 

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