C++ Solution


  • 0
    F

    Check whether adjacent next number exceeds previous number by 1 and continue until the difference is not 1. If a number with difference of 1 is found, the starting number won't be same as last number in a range.

    vector<string> summaryRanges(vector<int>& nums) {
            vector<string> vec;
            int a;
            int i=0, n=nums.size();
            for(;i<n;i++){
                a=nums[i];
                while((i<n-1) && ((nums[i+1]-nums[i])==1)){
                    i++;                
                }
                if(a!=nums[i]){
                    vec.push_back(to_string(a)+"->"+to_string(nums[i]));
                }
                else
                    vec.push_back(to_string(a));
            }
            return vec;   
        }
    

Log in to reply
 

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