9ms C++ clean code


  • 0
    J
    class StringIterator {
    private:
        string str;
        int pos;
        int count;
        int c;
    public:
        StringIterator(string compressedString) {
            str=compressedString;
            pos=0;
            count=0;
        }
        
        char next() {
            if(pos>=str.length() && count==0)
             return ' ';
            if(count==0)
            {
                c=str[pos];
                ++pos;
                while(pos<str.length() && isdigit(str[pos]))
                {
                    count=count*10+str[pos]-'0';
                    ++pos;
                }
            }
            
            --count;
            return c;
        }
        
        bool hasNext() {
            if(pos>=str.length() && count==0)
                return false;
            
            return true;
        }
    };
    

Log in to reply
 

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