225 Implement Stack using Queues(用队列实现栈Medium)
2015-06-17 09:36
465 查看
题目意思:用队列实现栈,push(),pop(),top(),empty()
思路:用两个queue,pop时将一个queue的元素pop再push到另一个队列,queue只留最后一个元素,并pop,再将目标队列变为另一个
ps:用栈实现队列,参考剑指offer
思路:用两个queue,pop时将一个queue的元素pop再push到另一个队列,queue只留最后一个元素,并pop,再将目标队列变为另一个
ps:用栈实现队列,参考剑指offer
class Stack { private: queue<int> q[2]; int flag=0; public: // Push element x onto stack. void push(int x) { q[flag].push(x); } // Removes the element on top of the stack. void pop() { while(q[flag].size()>1){ q[1-flag].push(q[flag].front()); q[flag].pop(); } q[flag].pop(); flag=1-flag; } // Get the top element. int top() { return q[flag].back(); } // Return whether the stack is empty. bool empty() { if(q[flag].empty()){ return true; } return false; } };
相关文章推荐
- 一个大量Burpsuite插件的github
- IOS UI segmentedControl UISegmentedControl 常见属性和用法
- Maven异常Type Project configuration is not up-to-date with pom.xml. Run Maven->Update Project or use Quick Fix
- HBuild+Mui学习笔记(二):一些知识点
- iOS 9 新增 UIStackView 官方文档翻译
- 怎么创建health bar 使用 uGUI
- Leetcode Repeated DNA sequences
- Wix 安装部署教程(十一) ---QuickWix
- Maven build error on maven install
- 多线程之ArrayBlockingQueue的使用
- iOS开发-UILabel和UIButton添加下划线
- listener does not currently know of service requested in connect descriptor
- OpenProcessToken,LookupPrivilegeValue,AdjustTokenPrivileges提升进程权限
- MBaaS – IBM Mobile Cloud Services, Bluemix & MobileFirst
- Xcode6中segue取消原push与modal(deprecated)
- stroryboard利用Segue实现界面传值
- UILocalNotification 本地通知的应用实例
- 设计初学者如何快速进步?
- JSP deleteCategory.jsp (line: 9, column: 67) The JSP specification requires that an attribute name i
- UIButton的一些基本用法