232. Implement Queue using Stacks
2016-06-08 11:40
363 查看
题目:https://leetcode.com/problems/implement-queue-using-stacks/
代码:
代码:
class MyQueue { Queue<Integer> stack = new LinkedList<>(); // Push element x to the back of queue. public void push(int x) { Queue<Integer> temp = new LinkedList<>(); while(!stack.isEmpty()) { temp.add(stack.poll()); } temp.add(x); while(!temp.isEmpty()) { stack.add(temp.poll()); } } // Removes the element from in front of queue. public void pop() { stack.poll(); } // Get the front element. public int peek() { return stack.peek(); } // Return whether the queue is empty. public boolean empty() { return stack.isEmpty(); } }
相关文章推荐
- ArrayDeque
- leetcode(300)—— Longest Increasing Subsequence(最长递增子序列)
- 关于github上的pull request的翻译问题
- easyui datagrid 行右键 动态获取并生成toolbar 按钮
- 对require使用的一点理解
- iOS: 玩转UICollectionViewLayout
- jadx-gui反编译apk
- 生产项目中queue同步问题导致项目部署后CPU爆表问题解决
- 从request获取各种路径总结
- Fuel快速安装开源openstack的实践
- mysql索引类型Normal,Unique,Full Text区别及索引方法Btree,Hash的区别
- ProcessBuilder执行本地命令
- iOS开发概述UIkit动力学
- UITextField 设置左右视图、文字距离及字符长度限制
- UIImage两个初始化的区别
- Gengxin讲STL系列——Queue和Stack
- vue.js 之道vuex
- iOS App开发中的UISegmentedControl分段组件用法总结
- Gradle Build速度加快终极方法
- 用array_count_values统计一篇英文文档中每个单词的出现次数,结果用表格展示出来