LeetCode225 Implement Stack using Queues
Implement the following operations of a stack using queues. (Easy)
- 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).
分析:
使用两个队列实现,其中一个队列为空,用来进行颠倒顺序和输出队尾元素。
入栈操作: 向非空队列内入队即可
出栈操作:将非空队列除队尾元素外的所有元素导入另一个空队列,剩余队尾元素即为待应该待出栈元素
top()操作: 同出栈,注意只需访问返回,不需要让其出队,即仍需将其导入另一队列
注意:两队列地位平等,均可能用作储存和转移工作
代码:
class Stack {
private:
queue<int> q1,q2;
public:
// Push element x onto stack.
void push(int x) {
if(!q1.empty()){
q1.push(x);
}
else{
q2.push(x);
}
}
// Removes the element on top of the stack.
void pop() {
if(!q1.empty()){
while(q1.size() > ){
q2.push(q1.front());
q1.pop();
}
q1.pop();
}else{
while(q2.size() > ){
q1.push(q2.front());
q2.pop();
}
q2.pop();
}
}
// Get the top element.
int top() {
if(!q1.empty()){
while(q1.size() > ){
q2.push(q1.front());
q1.pop();
}
int ans = q1.front();
q2.push(q1.front());
q1.pop();
return ans;
}else{
while(q2.size() > ){
q1.push(q2.front());
q2.pop();
}
int ans = q2.front();
q1.push(q2.front());
q2.pop();
return ans;
}
}
// Return whether the stack is empty.
bool empty() {
if(q1.empty() && q2.empty()){
return true;
}
return false;
}
};
LeetCode225 Implement Stack using Queues的更多相关文章
- 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( ...
- 232. Implement Queue using Stacks,225. Implement Stack using Queues
232. Implement Queue using Stacks Total Accepted: 27024 Total Submissions: 79793 Difficulty: Easy Im ...
- 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 ...
- 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) -- ...
- [Swift]LeetCode225. 用队列实现栈 | 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 ...
- Java for LeetCode 225 Implement Stack using Queues
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- Implement Stack using Queues
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
随机推荐
- mysql 如果处理货币金钱类型
我们知道,数据库存金钱类型可以float.double.decimal ,相比较而已decimal 最好用. 好吧, 我们公司用的分为单位, 类型 用bigint 存取,操作的时候到是很方便, 展示的 ...
- Spring-session整合到Redis
闲来无事,学习一下spring的session管理,作为一个初学者,我了解到了如下内容: 1.为何要用Spring-session 在传统单机web应用中,一般使用tomcat/jetty等web容器 ...
- spring boot项目启动报DataSource错误
初建一个简单的spring boot 项目,启动后会报错. Exception encountered during context initialization - cancelling refre ...
- lumen使用CORS解决跨域问题
因为公司的业务是前后端分离,web前端和后端接口域名不同,所以存在跨域问题,开始使用的是jsonp解决,但是因为接口风格是rest的,还有delete.put等请求,jsonp就不够用了(涉及HTTP ...
- create_pascal_tf_record.py 生成的record一直为0字节
后面发现这个错误原来是自己Main目录下的train.txt中间没东西
- 更好用的集群限流功能,Sentinel 发布 v1.4.2
摘要: 感谢 Sentinel 社区的贡献者们 ️ Sentinel 发布 v1.4.2 正式发布,该版本主要变更如下: 特性/功能改进 新增 Zuul 1.x 适配模块(sentinel-zuul- ...
- Linux下perl模块安装
perl模块下载地址: http://search.cpan.org/ 假设放在/usr/local/src/下 cd /usr/local/src 上传下载的压缩包CGI-Session-3.95. ...
- 一些js面试高频知识点的总结
第一部分:Object Prototypes (对象原型) (1)定义一个方法,要求传入一个string类型的参数,然后将string的每个字符间加个空格返回,例如: spacify('hello w ...
- 数组的方法之(Array.prototype.forEach() 方法)
forEach() 方法对数组的每个元素执行一次提供的函数. 注意: 没有返回一个新数组 并且 没有返回值! 应用场景:为一些相同的元素,绑定事件处理器! const arr = ['a', 'b', ...
- Intent传递list集合时异常解决
以前只是用intent传递一些简单的值,最近传递list集合时发现值总是传不过去,logcat报如下错误 说的是不能处理值为null的情况,回过头看list集合时确实发现有value为null的key ...