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 empty
operations 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 ...
随机推荐
- [javaEE] javaweb的mvc设计思想
Servlet:在Servlet中拼接html内容 JSP:在html中拼接java JSP+JavaBean:利用javaBean将大量的代码提取走 Servlet+JSP+JavaBean:Ser ...
- JAVA_SE_Day02 String 的正则表达式
字符串支持正则表达式的方法一: boolean matches(String regex) 注意: 给定的正则表达式就算不指定边界符(^,$),也会全匹配验证 空字符串和null 空字符串是看不见,而 ...
- spring-boot-starter-data-elasticsearch实现es的增删查改
首先,必须吐槽一下,springboot这个elasticsearch包对于elasticsearch的支持十分不友好,目前只支持很低版本的elasticsearch,如果有哪位大牛知道如何兼容更高版 ...
- Golang 的 TOML库
TOML 的全称是 Tom's Obvious, Minimal Language,因为它的作者是 GitHub 联合创始人 Tom Preston-Werner. TOML 的目标是成为一个极简的配 ...
- mybatis笔记<一> Demo
mybatis作为一个orm互联网公司基本都在用,今天写个笔记.记录一下mybatis使用 参考官网:http://www.mybatis.org/mybatis-3/getting-started. ...
- ZT 为什么Java中继承多数是有害的?
大多数好的设计者象躲避瘟疫一样来避免使用实现继承(extends 关系).实际上80%的代码应该完全用interfaces写,而不是通过extends.“Java设计模式”一书详细阐述了怎样用接口继承 ...
- JAVA SwingWorkder的使用例
最近在学习Swing,我们都知道在UI表现线程里面长时间执行操作时,画面会假死,为了能够让费时操作不影响画面表现,就需要用多线程了.首先考虑的就是Swing内部的 SwingWorkder对象,但是网 ...
- Django—Cookie and Session
一.Cookie Cookie,有时也用其复数形式 Cookies,指某些网站为了辨别用户身份.进行 session 跟踪而储存在用户本地终端上的数据(通常经过加密). 1. 应用 服务器可以利用Co ...
- shp文件导入mysql5.6.15
百度了一下 方法大致相同,就是提供的资源都缺斤短两还在细节上有差异.所以上传一份正确的. 0.将cygwin1.dll拷贝到system32目录下面1.将shp以及shp的相关文件和DOShere的d ...
- Java基础之异常处理机制
在Java中,异常分为编译时异常和运行时异常. 编译时异常又叫编译时被监测的异常:在程序编译过程中监测到非运行时异常的异常,出现该异常要么向上抛出,要么捕获处理.运行时异常(runtimeExcept ...