其他分享
首页 > 其他分享> > 【剑指 Offer09】用两个栈实现队列

【剑指 Offer09】用两个栈实现队列

作者:互联网

/**
 * 剑指 Offer 09. 用两个栈实现队列
 * https://leetcode.cn/problems/yong-liang-ge-zhan-shi-xian-dui-lie-lcof/
 * */
public class CQueue {
    private Deque<Integer> stack1;
    private Deque<Integer> stack2;

    public CQueue() {
        stack1 = new ArrayDeque<>();
        stack2 = new ArrayDeque<>();
    }

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

    public int deleteHead() {
        if (stack2.isEmpty()) {
            while (!stack1.isEmpty()) {
                stack2.push(stack1.pop());
            }
        }
        if (!stack2.isEmpty()) {
            return stack2.pop();
        }
        return -1;
    }
}

标签:return,队列,Offer09,private,实现,isEmpty,stack2,public,stack1
来源: https://www.cnblogs.com/liaozibo/p/offer-09.html