My solution but complex


  • 0
    V
    class Solution {
    public:
        vector<string> summaryRanges(vector<int>& nums) {
            vector<string> result;
        
            int i = 0;
            while( i < nums.size() )
            {
                int left = nums[ i ];
                stringstream ss;
                ss << left;
            
                int j = 1;
                for( i++; i < nums.size(); i++ )
                {
                    if( left != nums[ i ] - j )
                        break;
                    j++;
                }
                if( j != 1 )
                    ss << "->" << nums[ i - 1 ];
    
                string temp;
                ss >> temp;
                result.push_back( temp );
            }
            return result;
        }
    };

Log in to reply
 

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