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) -- ...
随机推荐
- mouseover和mouseout多次触发解决方法(兼容ie和firefox)(转)
在用到mouseover和mouseout事件来作为事件触发的条件,但是如果我们用做触发的元素内部有其他的元素的时候当鼠标移上的时候会反复的触发mouseover和mouseout事件,如导致菜单闪烁 ...
- ios基础篇(九)——自定义UITabBar
上一篇讲到了UITabBarViewController,接着说说UITabBarViewController中怎么自定义TabBar. 今天仿写了微博,发现底部tabbar中间的button和其他有 ...
- jdk版本比较
JDK各个版本的新特性 对于很多刚接触java语言的初学者来说,要了解一门语言,最好的方式就是要能从基础的版本进行了解,升级的过程,以及升级的新特性,这样才能循序渐进的学好一门语言.今天先为大家介绍一 ...
- qml package 的使用
什么时候使用这个.就是多个view使用同一个deleagte的时候. The Package class is used in conjunction with VisualDataModel to ...
- weblogic被锁解决方案
weblogic被锁,无法启动. 解决方案:http://blog.csdn.net/zhengqiqiqinqin/article/details/17025741
- js计算日期的前几天的日期
月份0---11 var date = new Date(year,fenye_arr[0]-1,fenye_arr[1]); miao=date.getTime(); var ...
- 算法导论-钢条切割 C# 递归实现
下班前看到有位兄弟写 钢条切割问题,尝试实现C#版, 还没有实现最优版,分享一下. int[] parr; private void button1_Click(object sender, Even ...
- 数据结构-bubble sort
#gcc version 4.5.3 (GCC) #include <iostream> #include <algorithm> template <typename ...
- 【C语言学习】-04 一维数组、字符数组
一维数组.数组排序.字符数
- Android VersionedGestureDetector手势事件
今天研究了一下PhotoView,发现里面的自定义的手势事件可以支持所有的SDK版本,该事件可以实现拖拽.滑动.缩放功能.下面直接上代码: public abstract class Versione ...