my C++ queue solution


  • 0
    J
    class StringIterator {
    public:
        StringIterator(string compressedString) {
            int num = 0;
            char c;
            for(int i = 0;i<compressedString.size();i++)
            {
                if(!isdigit(compressedString[i]))
                {
                    if(num!=0) q.push({c,num});
                    num = 0;
                    c = compressedString[i];
                }
                else
                    num = num*10+compressedString[i]-'0'; 
            }
            if(num!=0) q.push({c,num});
        }
        
        char next() {
            if(!hasNext()) return ' ';
            char val = q.front().first;
            q.front().second--;
            if(q.front().second==0)q.pop();
            return val;
        }
        
        bool hasNext() {
            return q.size()>0?true:false;
        }
    private:
        queue<pair<char,int>> q;
        bool isdigit(char c)
       {
           return c>='0'&&c<='9';
       }
    };
    

Log in to reply
 

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