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 ...
随机推荐
- ASP.NET MVC4 新手入门教程之七 ---7.向电影模式和表中添加新字段
在这一节中,您将使用实体框架代码第一次迁移,迁移到模型类的一些变化,所以该更改应用于数据库. 默认情况下,当您使用实体框架代码优先将自动创建一个数据库,像你那样早些时候在本教程中,代码第一次添加一个表 ...
- Asp.NET MVC4 + Ajax 实现多文件上传
本文转自http://www.cnblogs.com/freeliver54/archive/2013/05/15/3079700.html JS部分测试可以,jQuery部分没有测试先留着 HTML ...
- 用 Redis Desktop Manager 远程连接 redis 数据库。
环境: 本机OS:window 10(本机没有安装redis) redis 服务器:centos 7 使用 Redis Desktop Manager 工具远程连接 redis. Redis Desk ...
- 提示"No 'Access-Control-Allow-Origin' header"及Spring 中解决跨域问题
问题描述 No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://12 ...
- Spring cloud ReadTimeout 问题解决
今天使用Spring cloud @FeignClient 调用远程服务的时候,出现readTimeout问题,通过找资料解决方式如下 在Spring.properties 配置文件中添加如下属性解决 ...
- 四 Scatter/Gather
scatter/gather用于描述从Channel中读取或者写入到Channel的操作. 分散(scatter):从Channel中读取在读操作中将读取的数据写入多个Buffer中.因此,Chann ...
- vim命令“=”、“d”、“y”的用法(结合光标移动命令,一些场合会非常方便)
vim有许多命令,网上搜有一堆贴子.文章列举出各种功能的命令. 对于“=”.“d”.“y”,我在无意中发现了它们所具有的相同的一些用法,先举以下三个例子: =nG dnG ynG 其中,n为行号.注意 ...
- Ubuntu 中QT 用sogou拼音 安装
1.下载搜狗输入法的安装包 下载地址为:http://pinyin.sogou.com/linux/ ,如下图,要选择与自己系统位数一致的安装包,我的系统是64位,所以我下载64位的安装包 2.按键C ...
- 关于cn.jedisoft.framework.annotations 的增删改查
今天在做一个crud的功能的时候,调用api老是调用不同.奇怪的是 在add的时候能添加进去,而删除和修改的时候不能成功. 最后反应过来,我在修改和删除的时候用的主键id是int类型的,接口类型是不能 ...
- 插入mysql语句报错:1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near
插入一个很简单的sql语句时候,mysql一直报错: [SQL] INSERT INTO ORDER ( id, activity_id, order_type, phone, order_amoun ...