LeetCode OJ 之 Implement Stack using Queues(使用队列实现栈)
来源:互联网 发布:淘宝的直通车在哪里 编辑:程序博客网 时间:2024/06/15 03:33
题目:
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.
- You must use only standard operations of a queue -- which means only
push to back
,peek/pop from front
,size
, andis 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).
思路:
1、使用两个队列实现。参考:http://blog.csdn.net/u012243115/article/details/45440645。
2、使用一个队列实现。
代码1:
class Stack {public: queue<int> q1; queue<int> q2; // Push element x onto stack. void push(int x) { if(!q2.empty()) q2.push(x); else q1.push(x); } // Removes the element on top of the stack. void pop() { if(q1.empty() && !q2.empty()) { size_t len = q2.size(); while(len != 1) { q1.push(q2.front()); q2.pop(); len--; } q2.pop(); return; } else { if(q2.empty() && !q1.empty()) { size_t len2 = q1.size(); while(len2 != 1) { q2.push(q1.front()); q1.pop(); len2--; } q1.pop(); return; } } } // Get the top element. int top() { if(q1.empty() && !q2.empty()) { size_t len = q2.size(); while(len != 1) { q1.push(q2.front()); q2.pop(); len--; } int tmp = q2.front(); q2.pop(); q1.push(tmp); return tmp; } else { if(q2.empty() && !q1.empty()) { size_t len2 = q1.size(); while(len2 != 1) { q2.push(q1.front()); q1.pop(); len2--; } int tmp2 = q1.front(); q1.pop(); q2.push(tmp2); return tmp2; } } } // Return whether the stack is empty. bool empty() { return q1.empty() && q2.empty(); }};
代码2:
class Stack {public: queue<int> q; // Push element x onto stack. void push(int x) { q.push(x); } // Removes the element on top of the stack. void pop() { if(q.empty()) return; int len = q.size(); for(int i = 1 ; i < len ; i++) { int tmp = q.front(); q.pop(); q.push(tmp); } q.pop(); } // Get the top element. int top() { return q.back(); } // Return whether the stack is empty. bool empty() { return q.empty(); }};
代码3:
不使用queue的back
class Stack {public: queue<int> q; // Push element x onto stack. void push(int x) { q.push(x); } // Removes the element on top of the stack. void pop() { if(q.empty()) return; int len = q.size(); for(int i = 1 ; i < len ; i++) { int tmp = q.front(); q.pop(); q.push(tmp); } q.pop(); } // Get the top element. int top() { int len = q.size(); for(int i = 1 ; i < len ; i++) { int tmp = q.front(); q.pop(); q.push(tmp); } int tmp = q.front(); q.pop(); q.push(tmp); return tmp; } // Return whether the stack is empty. bool empty() { return q.empty(); }};
0 0
- LeetCode OJ 之 Implement Stack using Queues(使用队列实现栈)
- Leetcode 225 Implement Stack using Queues 使用队列实现栈
- LeetCode 225. Implement Stack using Queues(使用队列来实现栈)
- LeetCode Implement Stack using Queues 栈&队列
- 两个队列实现一个栈[leetcode]Implement Stack using Queues
- leetcode 225. Implement Stack using Queues 双队列实现栈
- LeetCode学习之-225. 利用队列实现堆栈(Implement Stack using Queues)
- 栈-Implement Stack using Queues(用队列实现栈)
- 225. Implement Stack using Queues (用队列实现栈)
- LeetCode 225 Implement Stack using Queues(用队列来实现栈)(*)
- [LeetCode-225] Implement Stack using Queues(两个队列实现栈)
- LeetCode 225 Implement Stack using Queues(利用队列实现栈)
- leetcode oj java Implement Stack using Queues
- <LeetCode OJ> 225. Implement Stack using Queues
- 【LeetCode OJ 225】Implement Stack using Queues
- leetcode oj java Implement Stack using Queues
- Implement Stack using Queues 用队列实现栈
- 225.Implement Stack using Queues (用队列实现栈)
- MapReduce排序及实例
- 网络协议(UDP与TCP协议总结)
- 游戏感:虚拟感觉的游戏设计师指南——第十一章 规则的测量方法
- 黑马程序员-------异常处理和常用类
- SimpleXML应用案例一
- LeetCode OJ 之 Implement Stack using Queues(使用队列实现栈)
- sql中delete,drop ,truncate都代表删除,3者的区别总结
- 关于本地vss无法访问服务器问题
- 图论基础【待更新】
- 游戏感:虚拟感觉的游戏设计师指南——第十二章 Asteroids(小行星)
- 解决Eclipse使用JRE而非JDK的办法
- [java]生成带logo图片的二维码、以及二维码解码
- 【Android UI设计与开发】第08期:底部菜单栏(三)Fragment+FragmentTabHost实现仿新浪微博底部菜单栏
- Slow HTTP Denial of Service Attack 漏洞解决办法