[604. Design Compressed String Iterator] C++_AC


  • 0
    class StringIterator {
    public:
    string s;
    int i = 0;
    int count = 0;
    char cur = ' ';
    StringIterator(string compressedString) {
        s = compressedString;
    }
    char next() {
        if(count){
            count--;
        }else{
            if(hasNext()){
                cur = s[i++];
                count = 0;
                while(i < s.size() && !isalpha(s[i])){
                    count = count*10 + (s[i++] - '0');
                }
                count--;
            }
        }
        return cur;
    }
    
    bool hasNext() {
        if(i < s.size() || count > 0){
            return true;
        }
        cur = ' ';
        return false;
    }
    };
    

    /**

    • Your StringIterator object will be instantiated and called as such:
    • StringIterator obj = new StringIterator(compressedString);
    • char param_1 = obj.next();
    • bool param_2 = obj.hasNext();
      */

Log in to reply
 

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