LeetCode OJ: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)
类似上一题的两个栈实现队列,但是有点不同,具体的见下面的注释:
class Stack {
public:
// Push element x onto stack.
void push(int x) {
q1.push(x);
}
// Removes the element on top of the stack.
void pop() {
int tmp = q1.front();//注意这里的实现和双栈实现队列的方式是不相同的
q1.pop();
while(!q1.empty()){
q2.push(tmp);
tmp = q1.front();
q1.pop();
}
swap(q1,q2);
}
// Get the top element.
int top() {
int tmp = q1.front();
q1.pop();
while(!q1.empty()){
q2.push(tmp);
tmp = q1.front();
q1.pop();
}
q2.push(tmp);
swap(q1, q2);//用swap即可,无须再向上次那样再倒回去
return tmp;
}
// Return whether the stack is empty.
bool empty() {
return q1.empty();
}
private:
queue<int> q1;
queue<int> q2;
};
LeetCode OJ:Implement Stack using Queues(队列实现栈)的更多相关文章
- 225 Implement Stack using Queues 队列实现栈
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack.pop ...
- LeetCode 225 Implement Stack using Queues(用队列来实现栈)(*)
翻译 用队列来实现栈的例如以下操作. push(x) -- 将元素x加入进栈 pop() -- 从栈顶移除元素 top() -- 返回栈顶元素 empty() -- 返回栈是否为空 注意: 你必须使用 ...
- 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] 225. 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 用队列实现栈
1.两个队列实现,始终保持一个队列为空即可 class MyStack { public: /** Initialize your data structure here. */ MyStack() ...
- (easy)LeetCode 225.Implement Stack using Queues
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- LeetCode(36)- Implement Stack using Queues
题目: Implement the following operations of a stack using queues. push(x) -- Push element x onto stack ...
- 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 ...
- Leetcode 225 Implement Stack using Queues
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- Java [Leetcode 225]Implement Stack using Queues
题目描述: Implement the following operations of a stack using queues. push(x) -- Push element x onto sta ...
随机推荐
- 金融即服务(FaaS),将开启场景化金融新格局
转自: https://www.iyiou.com/p/28494/fs/1 [ 亿欧导读 ] 金融即服务揭示了场景金融的实现路径,通过双向连接做一个开放的系统,按需给客户提供金融服务. 本文系作者在 ...
- Nginx配置性能优化(转)
原文地址:http://blog.csdn.net/xifeijian/article/details/20956605 高层的配置 nginx.conf文件中,Nginx中有少数的几个高级配置在模块 ...
- python_threading模块实现多线程详解(转)
综述 Python这门解释性语言也有专门的线程模型,Python虚拟机使用GIL(Global Interpreter Lock,全局解释器锁)来互斥线程对共享资源的访问,但暂时无法利用多处理器的优势 ...
- 页面调用dll
1:本地安装vs2010,有时报错缺少WindowsSP1更新包,下载安装即可. 2:安装完成后,打开VS2010, 3:文件--新建--项目,找到ATL项目(一种COM组件写法),输入项目名称,点击 ...
- 为什么修改Host不生效
开发验证的好好的功能,提测后经常有测试反应功能有bug.很多原因都是测试切换host没生效造成的,为什么切换host后刷新页面了也没生效呢? 不生效原因: Keep-Alive 服务器在响应头设置了 ...
- ArrayList原理分析(重点在于扩容)
首先,ArrayList定义只定义类两个私有属性: /** * The array buffer into which the elements of the ArrayList are stored ...
- xtrabackup备份脚本
背景:现网环境全备份脚本:基于xtrabackup命令 #!/bin/sh # MySQL端口 PORT=' # 备份用户 USER='bkpuser' PAWD='bkpuser' Time=`da ...
- 20145314郑凯杰 《Java程序设计》实验二 实验报告
20145314郑凯杰 <Java程序设计>实验二 实验报告 实验要求 完成实验.撰写实验报告,实验报告以博客方式发表在博客园,注意实验报告重点是运行结果,遇到的问题(工具查找,安装,使用 ...
- Linux服务器上ftp的搭建和使用
知识点: 1. FTP的简介.工作原理 2.在Linux上搭建FTP服务器 参考: 阿里云文档:https://help.aliyun.com/knowledge_detail/60152.html ...
- 数据结构实习 Problem H 迷宫的最短路径
数据结构实习 Problem H 迷宫的最短路径 题目描述 设计一个算法找一条从迷宫入口到出口的最短路径. 输入 迷宫的行和列m n 迷宫的布局 输出 最短路径 样例输入 6 8 0 1 1 1 0 ...