剑指 Offer 09. 用两个栈实现队列 +java中栈和队列的使用

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

题目链接

class CQueue {

   private Stack<Integer> sta1;
    private Stack<Integer> sta2;
    public CQueue() {
        sta1 = new Stack<>();
        sta2 = new Stack<>();
    }

    public void appendTail(int value) {
        while(!sta1.empty()){
            sta2.push(sta1.pop());
        }
        sta1.push(value);
        while(!sta2.empty()){
            sta1.push(sta2.pop());
        }
    }

    public int deleteHead() {
        if(sta1.empty())
            return -1;
        else return sta1.pop();
    }
}

/**
 * Your CQueue object will be instantiated and called as such:
 * CQueue obj = new CQueue();
 * obj.appendTail(value);
 * int param_2 = obj.deleteHead();
 */
上一篇:2019牛客暑期多校训练营(第七场)H. Pair(数位DP)


下一篇:相似字串:后缀数组(wzz模板理解),单调栈