提交 a1a6a7f6 编写于 作者: GreyZeng's avatar GreyZeng

update code

上级 34a8457b
......@@ -11,42 +11,43 @@ import java.util.Stack;
// 笔记:https://www.cnblogs.com/greyzeng/p/16631644.html
public class LeetCode_0232_ImplementQueueUsingStacks {
class MyQueue {
private Stack<Integer> popStack;
private Stack<Integer> pushStack;
private final Stack<Integer> push;
private final Stack<Integer> pop;
public MyQueue() {
popStack = new Stack<>();
pushStack = new Stack<>();
push = new Stack<>();
pop = new Stack<>();
}
public void push(int x) {
pushStack.push(x);
push.push(x);
}
public int pop() {
while (!pushStack.isEmpty()) {
popStack.push(pushStack.pop());
while (!push.isEmpty()) {
pop.push(push.pop());
}
int v = popStack.pop();
while (!popStack.isEmpty()) {
pushStack.push(popStack.pop());
int val = pop.pop();
while (!pop.isEmpty()) {
push.push(pop.pop());
}
return v;
return val;
}
public int peek() {
while (!pushStack.isEmpty()) {
popStack.push(pushStack.pop());
while (!push.isEmpty()) {
pop.push(push.pop());
}
int v = popStack.peek();
while (!popStack.isEmpty()) {
pushStack.push(popStack.pop());
int val = pop.peek();
while (!pop.isEmpty()) {
push.push(pop.pop());
}
return v;
return val;
}
public boolean empty() {
return pushStack.isEmpty();
return push.isEmpty();
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册