Implement Stack using Queues leetcode
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.
不用多想,可以使用两个queue来实现,代码如下
耗时在pop上
class Stack {
public:
// Push element x onto stack.
void push(int x) {
que1.push(x);
}
// Removes the element on top of the stack.
void pop() {
while (que1.size() > )
{
que2.push(que1.front());
que1.pop();
}
que1.pop();
while (!que2.empty())
{
que1.push(que2.front());
que2.pop();
}
}
// Get the top element.
int top() {
return que1.back();
}
// Return whether the stack is empty.
bool empty() {
return que1.empty();
}
private:
queue<int> que1;
queue<int> que2;
};
查看其它人的代码,发现居然可以使用一个queue来实现,实现思路非常巧妙,每一次push操作都将queue之前的元素都移到后面,使其保持栈的排序
如:
push 1
1
push 2
2 1
push 3
3 2 1
class Stack {
public:
queue<int> que;
// Push element x onto stack.
void push(int x) {
que.push(x);
for (int i = ; i<que.size() - ; ++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();
}
};
Implement Stack using Queues leetcode的更多相关文章
- Implement Stack using Queues ——LeetCode
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- 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 ...
- 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) -- ...
- [LeetCode] Implement Stack using Queues 用队列来实现栈
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- 【一天一道LeetCode】#225. Implement Stack using Queues
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Impleme ...
- LeetCode 225 Implement Stack using Queues(用队列来实现栈)(*)
翻译 用队列来实现栈的例如以下操作. push(x) -- 将元素x加入进栈 pop() -- 从栈顶移除元素 top() -- 返回栈顶元素 empty() -- 返回栈是否为空 注意: 你必须使用 ...
随机推荐
- 将 Callout 容器添加到移动设备应用程序中
在移动设备应用程序中,callout 是在应用程序顶部弹出的容器.该容器可以容纳一个或多个组件,并且支持不同类型的布局. callout 容器可以是模态或非模态容器.模态容器在其关闭之前接受所有的键盘 ...
- Swift3.0服务端开发(五) 记事本的开发(iOS端+服务端)
前边以及陆陆续续的介绍了使用Swift3.0开发的服务端应用程序的Perfect框架.本篇博客就做一个阶段性的总结,做一个完整的实例,其实这个实例在<Swift3.0服务端开发(一)>这篇 ...
- Activity生命周期完全解析
**转载请注明出处:http://www.cnblogs.com/landptf/p/6309108.html** 生命周期是个老生常谈的问题了,今天做个汇总,全当是记个笔记,以后查找起来方便一些.下 ...
- hessian原理解析一(客户端分析)
hessian 是一款开源的二进制远程通讯协议,使用简单方法提供了RMI功能,主要用于面向对象的消息通信. 优点:跨平台.多语言支持.使用简单 缺点:传递复杂对象性能会下降,不适合安全性高的应用 一 ...
- CodeForces758D
D. Ability To Convert time limit per test:1 second memory limit per test:256 megabytes input:standar ...
- HDU5879(打表)
Cure Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- Madifest文件详解
介绍 Madifest是个XML的描述文件,对于每个DLL有DLL的Manifest文件,对于每个应用程序Application也有自己的Manifest.对于应用程序而言,Manifest可以是一个 ...
- BOM数据基础 - Mobox物料编码管理及实现
1 企业现状 在企业日常经营过程中会产生大量的文档,如设计图纸.变更单.计算书.设计方案等,如果是制造企业还会产生大量的产品.组成产品的零部件等物料,这些数据在进入信息系统前都需要有一个唯一的标识,也 ...
- 谈谈getElementsByClassName()
HTML5中新增的一个方法getElementsByClassName(),但是并非所有浏览器有支持 因此我们构造一个方法兼容这个方法 <script type="text/javas ...
- Eclipse 报java.lang.UnsupportedClassVersionError: ("yourclass") bad major version at offset=6
报这个错误是指你的jar包或者class 的被编译的jdk版本比当前runtime的jdk版本高. 解决问题 1)如果是jar包,重新用jdk 1.6编译你的jar 包 2)如果是java文件或者项目 ...