LeetCode Implement Stack using Queues (数据结构)
题意:
用队列来实现栈。
思路:
没有什么捷径,纯粹模拟。但是用一个队列就够了。
class Stack {
/*
// Push element x onto stack.
void push(int x) { } // Removes the element on top of the stack.
void pop() { } // Get the top element.
int top() { } // Return whether the stack is empty.
bool empty() { }
*/
queue<int> que;
public:
void push(int x)
{
que.push(x);
}
void pop(void)
{
if(!que.empty())
{
int n=que.size(),pos=;
while(pos<n)
{
que.push(que.front());
que.pop();
pos++;
}
que.pop();
}
}
int top()
{
if(!que.empty())
{
int n=que.size(), pos=,x=;
while(pos<=n)
{
if(pos==n) x=que.front();
que.push(que.front());
que.pop();
pos++;
}
return x;
}
}
bool empty(void)
{
if(que.empty()) return true;
else return false;
}
};
AC代码
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
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 与 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) -- ...
随机推荐
- [转]Arrays.sort()你应该知道的事
以下内容转自: 原文链接: programcreek 翻译: ImportNew.com- 刘志军 译文链接: http://www.importnew.com/8952.html --------- ...
- [转载]Windows 7笔记本创建wifi热点供手机上网教程
用智能手机的朋友会发现这样一个问题,智能手机比普通手机上网更耗流量.这是因为智能手机应用(软件)丰富,而且大部分应用都会自动联网.为此,许多人每月包了上百M的流量套餐,但用的时候还是小心翼翼,生怕流量 ...
- VSS Plugin配置FAQ(翻译)[转]
前言(译者) 就个人的成长历程来说,刚参加工作用的是 CVS ,前前后后有接近三年的使用体验,从今年开始使用 SVN .总的来说我更喜欢 SVN ,用起来的确很方便,例如在本地源代码文件中加一个空格然 ...
- B’QConf(北京软件质量大会)记
下午从公司加班回来,顺路到淘宝(大望路)参加B'QConf(北京软件质量大会).淘宝所在的国家广告产业园原来是一个菜市场,已经有大约6年没有到那一带活动了.之所以记得这么清楚,是因为6年前曾经从那里的 ...
- RHEL 6.3安装(超级详细图解教程)[转载]
附:RHEL6.3下载地址 32位:http://rhel.ieesee.net/uingei/rhel-server-6.3-i386-dvd.iso 64位:http://rhel.iee ...
- LCD驱动 15-3
测试:1:make menuconfig去掉原来的驱动程序 Device Drivers ---> Graphics support ---> ...
- Program B--CodeForces 492B
Description Vanya walks late at night along a straight street of length l, lit by n lanterns. Consid ...
- android 禁止viewPager 滑动
public class ContainerViewPager extends MyViewPager { public ContainerViewPager(Context context, Att ...
- SharePoint 2013 开发——开发并部署webpart
博客地址:http://blog.csdn.net/FoxDave webpart我们就不详细阐述了,在APP的开发中,自定义属性设置通过APP webpart的URL查询字符串传递,它通过IFR ...
- About Closure
Closure被翻译为闭包,C++11引入了Lambda表达式支持Closure,JavaScript支持Closure,Objective C支持Blocks,他们都是Closure,名字各有不同, ...