(leetcode)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, andis 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.
用一个队列就可实现,看了很多例子觉得discuss里面有个朋友写的很简练,就用了他的丝路,和她基本一致
class Stack {
public:
// Push element x onto stack..
queue<int> a;
void push(int x) {
a.push(x);
}
// Removes the element on top of the stack.
void pop() {
size_t i = ;
size_t size = a.size();
for(i = ; i < size-; ++i)
{
a.push(a.front());
a.pop();
}
a.pop();
}
// Get the top element.
int top() {
return a.back();
}
// Return whether the stack is empty.
bool empty() {
return a.empty();
}
};
(leetcode)Implement Stack using Queues的更多相关文章
- [LeetCode] Implement Stack using Queues 用队列来实现栈
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- LeetCode——Implement Stack using Queues
Description: Implement the following operations of a stack using queues. push(x) -- Push element x o ...
- LeetCode Implement Stack using Queues (数据结构)
题意: 用队列来实现栈. 思路: 没有什么捷径,纯粹模拟.但是用一个队列就够了. class Stack { /* // Push element x onto stack. void push(in ...
- leetcode:Implement Stack using Queues 与 Implement Queue using Stacks
一.Implement Stack using Queues Implement the following operations of a stack using queues. push(x) - ...
- 【LeetCode】232 & 225 - Implement Queue using Stacks & Implement Stack using Queues
232 - Implement Queue using Stacks Implement the following operations of a queue using stacks. push( ...
- leetcode 155. Min Stack 、232. Implement Queue using Stacks 、225. Implement Stack using Queues
155. Min Stack class MinStack { public: /** initialize your data structure here. */ MinStack() { } v ...
- lc面试准备:Implement Stack using Queues
1 题目 Implement the following operations of a stack using queues. push(x) -- Push element x onto stac ...
- 232. Implement Queue using Stacks,225. Implement Stack using Queues
232. Implement Queue using Stacks Total Accepted: 27024 Total Submissions: 79793 Difficulty: Easy Im ...
- Implement Queue by Two Stacks & Implement Stack using Queues
Implement Queue by Two Stacks Implement the following operations of a queue using stacks. push(x) -- ...
随机推荐
- BZOJ3463 : [COCI2012] Inspector
考虑将序列分成$\sqrt{n\log n}$块,每块维护下凸壳,修改时在相应块打上需要修改的标记. 查询时,对于两端零散部分暴力查询. 对于中间的块,如果有修改标记,则暴力重构. 然后在凸壳上查询时 ...
- BZOJ2735 : 世博会
$|x_1-x_2|+|y_1-y_2|=\max(|(x_1+y_1)-(x_2+y_2)|,|(x_1-y_1)-(x_2-y_2)|)$ 将坐标$(x,y)$逆变换为$(\frac{x+y}{2 ...
- [译]JavaScript:函数的作用域链
原文:http://blogs.msdn.com/b/jscript/archive/2007/07/26/scope-chain-of-jscript-functions.aspx 在JavaScr ...
- [Unity2D]脚本基类MonoBehaviour介绍
Unity中的脚本都是继承自MonoBehaviour. MonoBehaviour 表示一个单一的行为.Unity中用户对游戏对象的操作被分割成若干个单一行为.每个单一行为都作为一个MonoBeha ...
- OI优化开关
#pragma comment(linker,"/STACK:10240000,10240000")#pragma GCC optimize ("O2")
- CentOS mysql硬盘满了挂载阿里云硬盘
前提,昨天晚上导入数据库到本地时候发现硬盘满了,出了,好多错,这边在目录下新建了一个/mysql这样的数据库目录,再将/etc/my.cnf 下的datadir 指向到/mysql下,就可以了 阿里云 ...
- lvs原理和实战
lvs-dr原理 lvs-nat原理: 当我们的网站流量越来越大时一台web服务器已经无法满足需求了,我们该如何解决呢??把服务器连接起来实现负载均衡或许是个不错的办法..下面我就来看看怎么实现吧 ...
- [转]C# WinForm动态调用远程Web服务
本文转自:http://blog.csdn.net/muyangjun/article/details/7930871 1.添加服务引用 2.在弹出的添加服务引用对话框地址栏中输入WebService ...
- HTML5 javascript CSS3 jQuery Mobile一些好用的网站
jQueryMobile:学习 http://www.runoob.com/jquerymobile/jquerymobile-tutorial.html 百度 CDN: http://cdn.cod ...
- Asp.Net:Repeater 详情 备用
页面 repeator就想for循环一样,没有编辑模板,有删除delete和详情detail模板 <%@ Page Language="C#" AutoEventWireup ...