重拾算法之剑指Offier——用两个栈实现队列
2017-02-09 10:09
281 查看
剑指Offier——用两个栈实现队列
题目描述用两个栈来实现一个队列,完成队列的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.empty()){ stack1.push(stack2.peek()); stack2.pop(); } stack1.push(node); } public int pop() { while (!stack1.empty()){ stack2.push(stack1.peek()); stack1.pop(); } int peek = stack2.peek(); stack2.pop(); return peek; } }
相关文章推荐
- 剑指offer-算法题练习:part5 用两个栈实现队列
- [剑指offer]算法5 两个栈实现1个队列
- 剑指offer-->面试题7 用两个栈实现队列
- 【九度OJ1512】|【剑指offer7】用两个栈实现一个队列
- 【剑指offer】面试题:两个队列实现栈
- [算法浅析] 用两个栈实现队列
- 【剑指offer】面试题7:用两个栈实现队列
- 数据结构与算法之—两个栈实现一个队列
- 用两个栈实现一个队列,算法和实现
- 每天一个小算法(6)---- 通过两个栈实现一个队列
- 剑指Offer - 九度1512 - 用两个栈实现队列
- 面试算法(六)用两个栈实现队列
- [剑指offer][面试题07]用两个栈实现队列
- 【剑指offer】面试题7:两个栈实现队列
- 用两个栈实现一个队列的功能?要求给出算法和思路!
- 简单算法 - 两个队列实现一个栈
- 算法导论10.1-7习题解答(用两个队列实现一个栈)
- 【每天学点算法题10.20】用两个栈实现队列
- 【剑指offer】用两个栈实现队列
- 剑指OFFER之用两个栈实现队列(九度OJ1512)