标签:队列 cal 两个栈 this import 返回 util instant class
用两个栈实现一个队列。队列的声明如下,请实现它的两个函数 appendTail 和 deleteHead ,分别完成在队列尾部插入整数和在队列头部删除整数的功能。(若队列中没有元素,deleteHead?操作返回 -1 )来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/yong-liang-ge-zhan-shi-xian-dui-lie-lcof
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
import java.util.Stack;
class CQueue {
    private Stack<Integer> s1;
    private Stack<Integer> s2;
    public CQueue() {
        this.s1 = new Stack<>();
        this.s2 = new Stack<>();
    }
    public void appendTail(int value) {
        s1.push(value);
    }
    public int deleteHead() {
        if (s2.isEmpty()) {
            while (! s1.isEmpty()) {
                s2.push(s1.pop());
            }
            if (s2.isEmpty()) {
                return -1;
            }
        }
        return s2.pop();
    }
}
/**
 * Your CQueue object will be instantiated and called as such:
 * CQueue obj = new CQueue();
 * obj.appendTail(value);
 * int param_2 = obj.deleteHead();
 */标签:队列 cal 两个栈 this import 返回 util instant class
原文地址:https://blog.51cto.com/tianyiya/2530932