lintcode&九章算法——Lintcode No.40 用栈实现队列 ? 待解决
2017-12-28 10:43
405 查看
原文:fourierhai
链接:http://mp.weixin.qq.com/s/ssAc1AodazaVtjRnFsTTgQ
Lintcode No.40 用栈实现队列
代码实现:
AC时间:在2500ms左右。
链接:http://mp.weixin.qq.com/s/ssAc1AodazaVtjRnFsTTgQ
Lintcode No.40 用栈实现队列
代码实现:
public class MyQueue{ private Stack<Integer> stack1; private Stack<Integer> stack2; public MyQueue() { // do intialization if necessary stack1 = new Stack<Integer>(); stack2 = new Stack<Integer>(); } /* * @param element: An integer * * @return: nothing */ public void push(int element) { // write your code here stack1.push(element); } /* * @return: An integer */ public int pop() { // write your code here if (stack2.empty()) { while (!stack1.empty()) { int s1 = stack1.pop(); stack2.push(s1); } } return stack2.pop(); } /* * @return: An integer */ public int top() { // write your code here if (stack2.empty()) { while (!stack1.empty()) { int s1 = stack1.pop(); stack2.push(s1); } } return stack2.peek(); } }
AC时间:在2500ms左右。
相关文章推荐
- lintcode&九章算法——No.97 二叉树的最大深度 ? 待解决
- lintcode&九章算法——No.50 数组剔除元素后的乘积 ? 待解决
- lintcode&九章算法——No.4 丑数(二) ? 待解决
- lintcode&九章算法——Google 面试题 | 字典里面的最长单词 ? 待解决
- lintcode&九章算法——No.50 数组剔除元素后的乘积 ? 待解决
- lintcode&九章算法——Google面试题 | 有效括号字符串 ? 待解决
- lintcode&九章算法——No.53 翻转字符串 ? 待解决
- lintcode&九章算法——Lintcode No.82 落单的数 ? 待解决
- lintcode&九章算法——Lintcode No.31 划分数组 ? 待解决
- lintcode&九章算法——100. 删除有序数组里的重复元素 ? 待解决
- lintcode&九章算法——Google 面试题 | 数组的度数 ? 待解决
- lintcode&九章算法——Microsoft 面试题 | 我能赢 ? 待解决
- lintcode&九章算法——No.11 二叉查找树中搜索区间 ? 待解决
- lintcode&九章算法——No.60 搜索插入位置 ? 待解决
- lintcode&九章算法——Lintcode No.96. 链表划分 ? 待解决
- lintcode&九章算法—— No.55 比较字符串 ? 待解决
- lintcode&九章算法——No.70 二叉树的层次遍历(二) ? 待解决
- lintcode&九章算法——Google 面试题 | 多余的连接 ? 待解决
- lintcode&九章算法——101. 删除有序数组里的重复元素 II ? 待解决
- lintcode&九章算法——lintcode NO.5 第k大元素 ? 带解决