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 ...
随机推荐
- js打印去掉页眉页脚
<style type="text/css" media="print"> @page /* 实现代码 */ { size: auto; /* au ...
- 使用axis2调用webservice需要导入的依赖
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> &l ...
- spring的事务管理配置
spring有两种事务配置器,可以使用spring的jdbc事务管理器,也可以使用对hibernate的事务管理器 第一种 使用Spring JDBC或IBatis进行事务配置(配置文件方式): &l ...
- Java面试题之HashMap阿里面试必问知识点,你会吗?
面试官Q1:你用过HashMap,你能跟我说说它的数据结构吗? HashMap作为一种容器类型,无论你是否了解过其内部的实现原理,它的大名已经频频出现在各种互联网Java面试题中了.从基本的使用角度来 ...
- poj Corn Fields 状态压缩dp。
Corn Fields Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 5763 Accepted: 3052 Descr ...
- dockerfile 踩坑记录
1.使用ADD/COPY命令 源文件必须和Dockfile位于同一目录下(使用绝对路径是没用的,会提示找不到你的源文件) 2.ADD命令会自动解压 3.尽量耗时且不容易变的部分放在dockerfile ...
- js 密码 正则表达式
1. 代码 function checkPassword(str){ var reg1 = /[!@#$%^&*()_?<>{}]{1}/; var reg2 = /([a-zA- ...
- 牛顿迭代,多项式求逆,除法,开方,exp,ln,求幂
牛顿迭代 若 \[G(F_0(x))\equiv 0(mod\ x^{2^t})\] 牛顿迭代 \[F(x)\equiv F_0(x)-\frac{G(F_0(x))}{G'(F_0(x))}(mod ...
- 005hystrix.stream信息聚合Turbine
1.POM配置 和普通Spring Boot工程相比,仅仅添加了Turbine和Spring Boot Starter Actuator依赖 <dependencies> <!--添 ...
- html dl dt dd 标签语法与使用
一.dl dt dd认识 html <dl> <dt> <dd>是一组合标 […]