C++ design 9ms


  • 0
    class StringIterator {
    public:
        string s;
        int head;
        int tail;
        int count;
        
        StringIterator(string compressedString): s(compressedString), head(0), tail(0), count(0) {}
        
        char next() {
            if(head>=s.size()) return ' ';
            if(count>0){
                count--;
                char c=s[head];
                if(count==0) head=tail;
                return c;
            }else{
                head=tail;
                if(head>=s.size()) return ' ';
                tail++;
                while(tail<s.size()&&isdigit(s[tail])) tail++;
                count=stoi(s.substr(head+1,tail-head-1))-1;
                return s[head];
            }
        }
        
        bool hasNext() {
            return head<s.size();
        }
    };
    

Log in to reply
 

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