您的位置:首页 > 产品设计 > UI/UE

[刷题]Implement Queue by Two Stacks

2015-09-03 17:36 441 查看
[LintCode]Implement Queue by Two Stacks

public class Solution {
private Stack<Integer> stack1;
private Stack<Integer> stack2;

public Solution() {
// 2015-09-03
stack1 = new Stack<>();
stack2 = new Stack<>();
}

public void push(int element) {
stack1.push(element);
}

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

public int top() {
if (stack2.isEmpty()) {
while (!stack1.isEmpty()) {
stack2.push(stack1.pop());
}
}
return stack2.peek();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: