leetcode-Implement Queue using Stacks
2015-11-04 22:24
465 查看
懒地打字。
class MyQueue { Stack<Integer> s; Stack<Integer> t; public MyQueue() { s=new Stack<Integer>(); t=new Stack<Integer>(); } // Push element x to the back of queue. public void push(int x) { s.push(x); } // Removes the element from in front of queue. public void pop() { int len=s.size(); for(int i=0;i<len;i++) { t.push(s.pop()); } t.pop(); int slen=t.size(); for(int j=0;j<slen;j++) { s.push(t.pop()); } } // Get the front element. public int peek() { int r = 0; int len = s.size(); for (int i = 0; i < len; i++) { t.push(s.pop()); } r=t.peek(); for (int i = 0; i < len; i++) { s.push(t.pop()); } return r; } // Return whether the queue is empty. public boolean empty() { return s.empty()&&t.empty(); } }
相关文章推荐
- MVC ueditor的使用(实现上传图片功能)
- Android消息机制 Handler,Looper,MessageQueue
- iOS UI 知识点
- 1017. Queueing at Bank (25)
- 1017. Queueing at Bank (25)
- 动态规划求最长递增子序列(longest increasing subsequence)
- hdoj3975Easy Fruit Ninja
- WinForm UI跨线程访问 Debug模式与非Debug模式下的区别
- zoj 1649 Rescue
- Longest Increasing Subsequence的两种算法
- 2015ACM/ICPC亚洲区长春站 L hdu 5538 House Building
- iOS开发小技巧:刷新UITableView
- UIview的一些基本方法
- XMPP框架 微信项目开发之Socket聊天室发送数据——获取键盘高度,修改控件的约束值,代码滚动UITabView到指定位置
- uiautomator使用
- Qt中用户界面的User Interface Compiler(uic)机制的相关说明
- UE激活
- CYC-UISearchBar 点击空白回收键盘
- UILabel自动适应尺寸
- LeetCode300——Longest Increasing Subsequence