<LeetCode OJ>Implement Stack using Queues【225】
2016-01-01 14:52
567 查看
225. Implement Stack using Queues
My SubmissionsQuestion
Total Accepted: 26676 Total
Submissions: 88051 Difficulty: Easy
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 emptyoperations 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).
Update (2015-06-11):
The class name of the Java function had been updated to MyStack instead of Stack.
Credits:
Special thanks to @jianchao.li.fighter for adding this problem and all test cases.
Subscribe to see which companies asked this question
Hide Tags
Stack Design
Hide Similar Problems
(E) Implement Queue using Stacks
//思路首先:感觉没什么特别好的办法,反正两个队列,始终保持某一个为空,始终留一个(这个就是栈顶元素)..... //申请两个队列1和2,举例,比如1,2,3,4压进队列1,则1是栈首,4才是低 //进栈:全部压进队列1 //取栈顶元素:将队列1的元素转给队列2,队列1只剩1个即可,此元素就是栈首,最后再次压入队列2,使队列1为空,也就是说始终保持队列1或者2为空 //弹出栈顶元素:如同取栈顶元素一样,留下的那一个弹出 //是否为空?显然来个队列为空就是空栈。 class Stack { public: // Push element x onto stack. void push(int x) { if(!q1.empty())//q1不为空 q1.push(x); else q2.push(x); } // Removes the element on top of the stack. void pop() { if(!q1.empty())//q1不为空 { while(q1.size()>1) { int val=q1.front(); q2.push(val); q1.pop(); } q1.pop(); } else { while(q2.size()>1) { int val=q2.front(); q1.push(val); q2.pop(); } q2.pop(); } } // Get the top element. int top() { if(!q1.empty())//q1不为空 { while(q1.size()>1) { int val=q1.front(); q2.push(val); q1.pop(); } int key= q1.front(); q1.pop(); q2.push(key); return key; } else { while(q2.size()>1) { int val=q2.front(); q1.push(val); q2.pop(); } int key=q2.front(); q2.pop(); q1.push(key); return key; } } // Return whether the stack is empty. bool empty() { return q1.empty() && q2.empty(); } private: queue<int> q1; queue<int> q2; };
对上面的程序简单优化了一下:
//思路首先:感觉没什么特别好的办法,反正两个队列,始终就是留一个..... //申请两个队列1和2,举例,比如1,2,3,4压进队列1,则1是栈首,4才是低 //进栈:全部压进队列1 //取栈顶元素:将队列1的元素转给队列2,队列1只剩1个即可,此元素就是栈首,最后再次压入队列2,使队列1为空,也就是说始终保持队列1或者2为空 //弹出栈顶元素:如同取栈顶元素一样,留下的那一个弹出 //是否为空?显然来个队列为空就是空栈。 class Stack { public: // Push element x onto stack. void push(int x) { if(!q1.empty())//q1不为空 q1.push(x); else q2.push(x); newtop=x;//新进来的元素就是栈顶元素,pop时一定要更新这个值 } // Removes the element on top of the stack. void pop() { if(!q1.empty())//q1不为空 { while(q1.size()>1) { int val=q1.front(); if(q1.size() == 2) newtop = val; q2.push(val); q1.pop(); } q1.pop(); } else { while(q2.size()>1) { int val=q2.front(); if(q2.size() == 2) newtop = val; q1.push(val); q2.pop(); } q2.pop(); } } // Get the top element. int top() { return newtop; } // Return whether the stack is empty. bool empty() { return q1.empty() && q2.empty(); } private: queue<int> q1; queue<int> q2; int newtop; };
来自别人家的思路:
//别人家的思路:每次数据压进来的时候,直接就将数据调整为栈的顺序 //即队首重新压倒队尾,操作que.size()-1次 //牛逼....... class Stack { public: queue<int> que; // Push element x onto stack. void push(int x) { que.push(x); for(int i=0;i<que.size()-1;++i){ que.push(que.front()); que.pop(); } } // Removes the element on top of the stack. void pop() { que.pop(); } // Get the top element. int top() { return que.front(); } // Return whether the stack is empty. bool empty() { return que.empty(); } };
相关文章推荐
- UIButton
- The requested URL /Users/add was not found on this server.
- android UI 优化之<viewstub>实现View的延迟加载
- Button 点击事件(参数)
- **Repeated DNA Sequences
- UIBarbutton点击事件问题
- Deep learning From Image to Sequence
- UILabel用法
- 【PAT】1007 Maximum Subsequence Sum (25) 最大连续子序列和 +记录位置
- emcee——Quickstart
- 项目管理神器——Confluence篇
- android UI 优化之<include>标签
- 关于UIView的autoresizingMask属性的研究
- 快速排序算法(Quicksort)
- 1.Basic Techniques and Knowledge
- NSValue的使用...@encode(type)...int strcmp(const char *, const char *); 返回 0 表示相等
- Android子线程中更新UI的三种方法和获取message的两种方法总结
- druid 数据库密码加密
- 自定义UITabBar的两种方式
- UILabel 的基本使用和扩展