7 lines C++ 0ms


  • 0
    J

    Reorganize the code with single loop

    
    vector<string> summaryRanges(vector<int>& nums) {
        vector<string> result;
        for (int i = 0, j = 0; j < nums.size(); j++)
            if ((j + 1 == nums.size()) || (nums[j] + 1 != nums[j + 1])) {
                result.emplace_back(to_string(nums[i]) + ((i == j) ? "" : "->" + to_string(nums[j])));
                i = j + 1;
            }
        return result;
    }
    

Log in to reply
 

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