Leetcode 225 两个队列实现栈
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).
Update (2015-06-11):
The class name of the Java function had been updated to MyStack instead of Stack.
Credits:
Special thanks to @jianchao.li.fighter for adding this problem and all test cases.
由于是队列,后进后出,栈顶元素在队列的尾部,结合一个flag判断栈顶在哪个队列。一开始两个队列都为空的时候,随便插入其中一个,比如q1,将q1的标志设为1,q2这时为空。如果要出栈,则将q1最后元素之前的所有元素出列,加入q2,并将标志设为q2,q1最后一个元素出列,此时q1为空.当要插入时,选择两者中为空的那个队列插入,并设标志为新插入的那个队列。
实现写了70+行,还需要优化
class Stack {
public:
// Push element x onto stack.
void push(int x) {
if (flag) //栈顶在队列1,继续加入
q1.push(x);
else
q2.push(x);
}
// Removes the element on top of the stack.
void pop() {
if (!empty()) {
if (flag) {//栈顶在队列1,将队列1转移到队列2
while (!q1.empty()) {
int temp = q1.front();
q1.pop();
if (q1.empty()) //弹出的是最后一个元素
flag = false; //栈顶在队列2
else //尚不是最后一个,则加入队列2
q2.push(temp);
}
}
else { //栈顶在队列2
while (!q2.empty()) {
int temp = q2.front();
q2.pop();
if (q2.empty()) //弹出的是最后一个元素
flag = true; //栈顶在队列1
else //尚不是最后一个,则加入队列1
q1.push(temp);
}
}
}
}
// Get the top element.
int top() {
int res = ;
if (flag) {//栈顶在队列1,将队列1转移到队列2
while () {
res = q1.front();
q1.pop();
if (q1.empty()) {//弹出的是最后一个元素
q1.push(res);//再压回去
break;
}
else //尚不是最后一个,则加入队列2
q2.push(res);
}
}
else { //栈顶在队列2
while () {
res = q2.front();
q2.pop();
if (q2.empty()) {//弹出的是最后一个元素
q2.push(res);
break;
}
else //尚不是最后一个,则加入队列1
q1.push(res);
}
}
return res;
}
// Return whether the stack is empty.
bool empty() {
return q1.empty()&&q2.empty();
}
queue<int> q1,q2;
bool flag = true; //true表示栈顶在队列1,false表示栈顶在队列2
};
其他博客的实现
http://blog.csdn.net/xy010902100449/article/details/49307823
http://blog.csdn.net/sunao2002002/article/details/46482673
Leetcode 225 两个队列实现栈的更多相关文章
- LeetCode 225:用队列实现栈 Implement Stack using Queues
题目: 使用队列实现栈的下列操作: push(x) -- 元素 x 入栈 pop() -- 移除栈顶元素 top() -- 获取栈顶元素 empty() -- 返回栈是否为空 Implement th ...
- LeetCode 225题用队列实现栈(Implement Stack using Queues) Java语言求解
链接 https://leetcode-cn.com/problems/implement-stack-using-queues/ 思路 首先演示push()操作:将元素依次进入队1,进入时用top元 ...
- 两个栈实现队列+两个队列实现栈----java
两个栈实现队列+两个队列实现栈----java 一.两个栈实现一个队列 思路:所有元素进stack1,然后所有出s ...
- Algorithm --> 两个栈实现队列和两个队列实现栈
两个栈实现队列和两个队列实现栈 队列(queue)先进先出的线性表:栈(stack)先进后出的线性表. 两个栈实现队列 法一思路: s1是入栈的,s2是出栈的. 入队列:直接压入s1即可: 出队列:如 ...
- 两个队列实现栈&两个栈实现队列(JAVA)
1,两个栈实现队列 题目描述 用两个栈来实现一个队列,完成队列的Push和Pop操作. 队列中的元素为int类型. 思路:栈的特点时先进后出,队列的特点是先进先出. 若此时有两个队列stack1,st ...
- 用两个栈实现队列与用两个队列实现栈(Python实现)
用两个栈实现队列: class QueueWithTwoStacks(object): def __init__(self): self._stack1 = [] self._stack2 = [] ...
- 剑指offer第二版面试题9:用两个队列实现栈(JAVA版)
题目:用两个队列实现栈. 分析:通过一系列的栈的压入和弹出操作来分析用队列模拟一个栈的过程,如图所示,我们先往栈内压入一个元素a.由于两个队列现在都是空,我们可以选择把a插入两个队列中的任一个.我们不 ...
- 编程题目: 两个队列实现栈(Python)
感觉两个队列实现栈 比 两个栈实现队列 麻烦 1.栈为空:当两个队列都为空的时候,栈为空 2.入栈操作:当队列2为空的时候,将元素入队到队列1:当队列1位空的时候,将元素入队到队列2: 如果队列1 和 ...
- (LeetCode)两个队列来实现一个栈
原题例如以下: Implement the following operations of a stack using queues. push(x) -- Push element x onto s ...
随机推荐
- git必会必知
1 前言 git前身是BitKeeper,但是他不是开源软件,不符合当时开源趋势,于是就会有了开源的git,git开发只用了十天时间.目前git是公司开发必不可少的一个工具,用于多人开发的分布式版本控 ...
- easyui前后台转义字符和普通字符的相互转换问题
昨天碰到一个问题,公司前端使用的是easyui和jquery,页面textarea编写了html代码,传入后台变成了<>类型代码,这样保存到数据库是没有问题的,但是在页面显示的时候需要显示 ...
- Thrift笔记(七)--回调源码分析
网上找了写代码,东拼西凑写了个demo.开始server用的是阻塞io,不行,换成非阻塞的io就可以.这里可能需要注意下 thrift文件 namespace java com.gxf.thrift ...
- laravel上传到七牛图片插件
1.首先引入两个插件 2.在https://developer.qiniu.com/kodo/sdk/1241/php找到安装命令 在终端运行composer require qiniu/php-sd ...
- 从零开始的全栈工程师——html篇1.5
列表与边距探讨和行块 一.列表 1.无序列表(UL) 1)内部必须有子标签<li></li>2)天生自带内外边距 p也是自带 大家会发现用UL的时候内容前面会出现一个像这样的一 ...
- makefile 模板 (template)
本文把makefile 分成了三份:生成可执行文件的makefile,生成静态链接库的makefile,生成动态链接库的makefile. 这些makefile都很简单,一般都是一看就会用,用法也很容 ...
- ToolBar 简单使用
ToolBar 简单使用 ToolBar 是在 android 5.0之后推出的一款用来替代 ActionBar 的 View.ActionBar 是Activity的一部分,不能用在其他视图层次上( ...
- c 读取整个文件内容
char* textFileRead(char* filename){char* text;FILE *pf = fopen(filename,"r");fseek(pf,0,SE ...
- C++模板详解(系转载,但是个人添加了一些内容)
原文地址:http://www.cnblogs.com/gw811/archive/2012/10/25/2738929.html 零.概述 模板是C++支持参数化多态的工具,使用模板可以使用户为类或 ...
- named 快速部署及主机记录普及
实验环境centos7.2,仅供参考 yum -y install epel-release --安装最新yum配置源 cd /etc/yum.repos.d/ # wget http://re ...