美文网首页
leetcode 剑指 Offer 09. 用两个栈实现队列

leetcode 剑指 Offer 09. 用两个栈实现队列

作者: Source_Chang | 来源:发表于2020-11-16 17:02 被阅读0次

leetcode

C++:

class CQueue {
public:
    std::stack<int> headStack;
    std::stack<int> tailStack;

public:
    CQueue() {

    }
    
    void appendTail(int value) {

        this -> tailStack.push(value);
    }
    
    int deleteHead() {

        if ( this -> headStack.empty() ) {

            while ( !( this -> tailStack.empty() ) ) {

                this -> headStack.push( this -> tailStack.top() );
                this -> tailStack.pop();
            }
        }

        if ( !( this -> headStack.empty() ) ) {

            int returnedValue = this -> headStack.top();
            this -> headStack.pop();

            return returnedValue;
        }

        return -1;
    }
};

/**
 * Your CQueue object will be instantiated and called as such:
 * CQueue* obj = new CQueue();
 * obj->appendTail(value);
 * int param_2 = obj->deleteHead();
 */

相关文章

网友评论

      本文标题:leetcode 剑指 Offer 09. 用两个栈实现队列

      本文链接:https://www.haomeiwen.com/subject/pinnbktx.html