首页 > 其他分享 >【栈】LeetCode 剑指 Offer 09. 用两个栈实现队列

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

时间:2023-03-08 20:12:47浏览次数:57  
标签:Offer 09 outStack public isEmpty inStack LeetCode

题目链接

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

思路

两个栈模拟

代码

class CQueue {
    Deque<Integer> inStack;
    Deque<Integer> outStack;

    public CQueue() {
        inStack = new ArrayDeque<Integer>();
        outStack = new ArrayDeque<Integer>();
    }

    public void appendTail(int value) {
        inStack.push(value);
    }

    public int deleteHead() {
        if(outStack.isEmpty()){
            if(inStack.isEmpty()){
                return -1;
            }
            in2out();
        }
        
        return outStack.pop();
    }

    private void in2out() {
        while(!inStack.isEmpty()){
            outStack.push(inStack.pop());
        }
    }
}

标签:Offer,09,outStack,public,isEmpty,inStack,LeetCode
From: https://www.cnblogs.com/shixuanliu/p/17195928.html

相关文章