Simple and easy c++ solution


  • 0
    C

    vector<string> summaryRanges(vector<int>& nums) {
    vector<string> vs;
    if (nums.size()==0) return vs;

    stringstream ss;
    int i=0,h;
    while (i<nums.size()){
        ss.str("");
        h=i;
        ss<<nums[h];
        while((i+1)<nums.size()&&nums[i+1]==nums[i]+1) i++;
        if (i>h){
            ss<<"->";
            ss<<nums[i];
        }
        vs.push_back(ss.str());
        i++;
    }
    return vs;
    

    }


Log in to reply
 

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