【leetcode C++】【剑指 Offer】 09. 用两个栈实现队列

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

在这里插入图片描述

class CQueue {
    
    
public:
    stack<int> S1, S2;
    CQueue() {
    
    
    }
    
    void appendTail(int value) {
    
    
        S1.push(value);
    }
    
    int deleteHead() {
    
    
        int ans = -1;
        if(S2.empty()) {
    
    
            while(!S1.empty()) {
    
    
                S2.push(S1.top());
                S1.pop();
            }
        }
        if(!S2.empty()) {
    
    
            ans = S2.top();
            S2.pop();
        }
        return ans;
    }
};

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

猜你喜欢

转载自blog.csdn.net/m0_37454852/article/details/114385523