My C++ solution Easy to understand 0ms


  • 0
    Q
    vector<string> summaryRanges(vector<int>& nums) {
            int size = nums.size();
            vector<string> result;
            if(!size)
                return result;
            for(int i = 0; i < size; i++){
                int tmp = nums[i];
                int j = i + 1;
                string s = "";
                bool flag = false;
                s = s + to_string(nums[i]);
                while(j < size){
                    if((nums[j]-j) > nums[i]-i)
                        break;
                    j++;
                    flag = true;
                }
                if(flag){
                        s = s + ("->" + to_string(nums[j-1]));
                }
                result.push_back(s);
                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.