[leetcode]Implement Stack using Queues
2015-11-20 09:56
453 查看
题目如下:
Implement the following operations of a stack using queues.
push(x) – Push element x onto stack.
pop() – Removes the element on top of the stack.
top() – Get the top element.
empty() – Return whether the stack is empty.
Notes:
You must use only standard operations of a queue – which means only push to back, peek/pop from front, size, and is empty operations are valid.
Depending on your language, queue may not be supported natively. You may simulate a queue by using a list or deque (double-ended queue), as long as you use only standard operations of a queue.
You may assume that all operations are valid (for example, no pop or top operations will be called on an empty stack).
就是对java中queue的复习,附上代码:
题目链接:`https://leetcode.com/problems/implement-stack-using-queues/
Implement the following operations of a stack using queues.
push(x) – Push element x onto stack.
pop() – Removes the element on top of the stack.
top() – Get the top element.
empty() – Return whether the stack is empty.
Notes:
You must use only standard operations of a queue – which means only push to back, peek/pop from front, size, and is empty operations are valid.
Depending on your language, queue may not be supported natively. You may simulate a queue by using a list or deque (double-ended queue), as long as you use only standard operations of a queue.
You may assume that all operations are valid (for example, no pop or top operations will be called on an empty stack).
就是对java中queue的复习,附上代码:
class MyStack { private Queue<Integer> myqueue; MyStack(){ myqueue = new LinkedList<Integer>(); } // Push element x onto stack. public void push(int x) { Queue tmp = new LinkedList<Integer>(); tmp.add(x); while(myqueue.size() != 0){ int data = myqueue.poll(); tmp.add(data); } myqueue = tmp; } // Removes the element on top of the stack. public void pop() { myqueue.poll(); } // Get the top element. public int top() { return myqueue.peek(); } // Return whether the stack is empty. public boolean empty() { return myqueue.size() == 0; } }
题目链接:`https://leetcode.com/problems/implement-stack-using-queues/
相关文章推荐
- iOS开发中使用Quartz2D绘图及自定义UIImageView控件
- 为移动而设计:交互设计篇
- 自定义zTree+easyui右键菜单
- Java遍历文件夹&读取指定格式的文件zUI金
- easyui combobox 下拉框后台列表查询
- APICloud 图片轮播模块 UIScrollPicture
- 导航控制器UINavigationController
- 导航控制器UINavigationController
- Kibana User Guide [4.2] » Visualize » Area Chartsl
- 联想VIBE UI 固件ROM刷机包集合
- Android 总结:Message,MessageQueue,Looper,Handler 消息机制
- 2、UITextField的方法和属性
- iOS 为什么必须在主线程中操作UI
- UIAlertView更换为UIAlerController
- 关于队列(queues)dispatchFrom,dispatchFromArray
- easyUI 提交按钮linkbutton失效和恢复设置
- 使用xib 自定义UITableViewCell 按钮无法点击事件
- UINavigationController
- UIView,UIButton,UIImageView等设置圆角,设置阴影,设置边框的方法
- UE4代码规范