Accepted c++ solution O(N) , 0ms running time.


  • 2
    J
    class Solution {
    public:
        vector<string> summaryRanges(vector<int>& nums) {
             string str="",arrow="->"; 
             int i;
             vector<string> res;
             if(nums.size()==0) return res;
             for(i=0;i<nums.size()-1;i++) {
                    if(nums[i]!=nums[i+1]-1) { 
                        if(str!="") str+="->";
                        str+=to_string(nums[i]);
                        res.push_back(str);
                        str="";
                    }
                    else 
                     if(str=="") str = to_string(nums[i]);
             } 
             if(str!="") str+="->";
             str+=to_string(nums[i]);
             res.push_back(str);
             return res;
        }
    };

Log in to reply
 

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