package leetcode.剑指Offer.用两个栈实现队列;
import java.util.Stack;
public class LeetCode {
public static void main(String[] args) {
}
}
class CQueue {
Stack<Integer> stack1 = new Stack<>();
Stack<Integer> stack2 = new Stack<>();
public CQueue() {}
public void appendTail(int value) {
stack1.push(value);
}
public int deleteHead() {
if (stack1.isEmpty() && stack2.isEmpty()) {
return -1;
}
if (stack2.isEmpty()) {
while (!stack1.isEmpty()) {
stack2.push(stack1.pop());
}
}
return stack2.pop();
}
}
image.png
网友评论