C++ Very Clean and 0ms


  • 0
    R
    string next( const string& val)
    {
        string result;
        
        for ( auto left_bound = val.cbegin(); left_bound != val.cend() ; /*no-op*/ )
        {
            auto right_bound = find_if( left_bound, val.cend(), bind2nd(not_equal_to<char>(),*left_bound) );
            result += '0' + ( right_bound-left_bound); // the trick here is that length is never > 9
            result += *left_bound;
            left_bound = right_bound;
        }
        
        return result;
    }
    
    string countAndSay(int n) 
    {
        string val="1";
        while (--n)
            val = next(val);
        return val;
    }

Log in to reply
 

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