ToB企服应用市场:ToB评测及商务社交产业平台
标题:
【剑指 Offer09】用两个栈实现队列
[打印本页]
作者:
千千梦丶琪
时间:
2022-8-9 14:44
标题:
【剑指 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;
}
}
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/)
Powered by Discuz! X3.4