牛客网-剑指offer-05-用两个栈实现队列
2017-09-12 16:32
288 查看
用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。
import java.util.Stack;
public class Solution {
Stack<Integer> stack1 = new Stack<Integer>();
Stack<Integer> stack2 = new Stack<Integer>();
public void push(int node) {
while(!stack2.isEmpty()) {
stack1.push(stack2.pop());
}
stack1.push(node);
}
public int pop() {
while(!stack1.isEmpty()) {
stack2.push(stack1.pop());
}
return stack2.pop();
}
}
import java.util.Stack;
public class Solution {
Stack<Integer> stack1 = new Stack<Integer>();
Stack<Integer> stack2 = new Stack<Integer>();
public void push(int node) {
while(!stack2.isEmpty()) {
stack1.push(stack2.pop());
}
stack1.push(node);
}
public int pop() {
while(!stack1.isEmpty()) {
stack2.push(stack1.pop());
}
return stack2.pop();
}
}
相关文章推荐
- 牛客网刷题--剑指offer(两个栈实现队列)
- 剑指Offer_05_用两个栈实现队列
- 剑指Offer [05] 用两个栈实现队列
- 牛客网 - 剑指Offer - 考点:栈和队列 题目:用两个栈实现队列
- [剑指OFFER] 用两个栈实现队列
- 剑指offer-用两个栈实现队列
- 剑指offer(C++)——用两个栈实现队列
- 剑指offer之面试题7:用两个栈实现队列
- 剑指offer 用两个栈实现队列 @python
- 剑指Offer面试题7用两个栈实现队列(附带用两个队列实现栈)
- [剑指Offer]9.用两个栈实现队列
- 【剑指offer】5.用两个栈实现队列
- 剑指offer-算法题练习:part5 用两个栈实现队列
- 【剑指offer-java版】5、用两个栈实现队列
- 剑指offer面试题7:用两个栈实现队列
- 剑指Offer——面试题7:用两个栈实现队列
- 剑指offer----用两个栈实现队列
- 剑指offer-07(1):两个队列实现栈
- 剑指offer——用两个栈实现队列
- 剑指offer 用两个栈实现队列