用两个队列去实现栈,这里我使用了队列数组q[2],在所有的过程中保证一个队列是空的

push时插入到空的队列中,然后将队列中的元素移到另一个队列中

pop时从不空的队列中pop()

peek时从不空的队列中取出front()

 class Stack {
public:
queue<int> q[]; // Push element x onto stack.
void move(int x){
while(!q[-x].empty()){
q[x].push(q[-x].front());
q[-x].pop();
}
} void push(int x) {
for(int i = ; i < ; ++i){
if(q[i].empty()){
q[i].push(x);
move(i);
break;
}
}
} // Removes the element on top of the stack.
void pop() {
for(int i = ; i < ; ++i){
if(!q[i].empty()){
q[i].pop();
break;
}
}
} // Get the top element.
int top() {
for(int i = ; i < ; ++i){
if(!q[i].empty()){
return q[i].front();
}
}
} // Return whether the stack is empty.
bool empty() {
return q[].empty() && q[].empty();
}
};

Leetcode 225 Implement Stack using Queues STL的更多相关文章

  1. LeetCode 225 Implement Stack using Queues(用队列来实现栈)(*)

    翻译 用队列来实现栈的例如以下操作. push(x) -- 将元素x加入进栈 pop() -- 从栈顶移除元素 top() -- 返回栈顶元素 empty() -- 返回栈是否为空 注意: 你必须使用 ...

  2. [LeetCode] 225. Implement Stack using Queues 用队列来实现栈

    Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...

  3. 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 ...

  4. (easy)LeetCode 225.Implement Stack using Queues

    Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...

  5. Leetcode 225 Implement Stack using Queues

    Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...

  6. Java [Leetcode 225]Implement Stack using Queues

    题目描述: Implement the following operations of a stack using queues. push(x) -- Push element x onto sta ...

  7. LeetCode 225 Implement Stack using Queues 用队列实现栈

    1.两个队列实现,始终保持一个队列为空即可 class MyStack { public: /** Initialize your data structure here. */ MyStack() ...

  8. leetcode 155. Min Stack 、232. Implement Queue using Stacks 、225. Implement Stack using Queues

    155. Min Stack class MinStack { public: /** initialize your data structure here. */ MinStack() { } v ...

  9. 232. Implement Queue using Stacks,225. Implement Stack using Queues

    232. Implement Queue using Stacks Total Accepted: 27024 Total Submissions: 79793 Difficulty: Easy Im ...

随机推荐

  1. linux运维笔记——常用命令详解diff

    1.diff 你可以把diff看成是linux上的文件比对工具 例子文件内容: [root@localhost disks]# cat test1.txt a b c d [root@localhos ...

  2. mvc深入理解

    对于v和c好理解, 对于model分为数据对象模型和业务逻辑模型,一般为一个类,数据对象模型包含对一个具体数据表的相关操作,业务逻辑模型为处理一些业务逻辑.

  3. iOS10适配及Xcode8配置

    一.证书管理 用Xcode8打开工程后,比较明显的就是下图了,这个是苹果的新特性,可以帮助我们自动管理证书.建议大家勾选这个Automatically manage signing(Ps.但是在bea ...

  4. java JVM垃圾回收机制

    Java语言出来之前,大家都在拼命的写C或者C++的程序,而此时存在一个很大的矛盾,C++等语言创建对象要不断的去开辟空间,不用的时候有需要不断的去释放控件,既要写构造函数,又要写析构函数,很多时候都 ...

  5. 小希的数表2(用的for循环)

    #include<iostream> 02.#include<algorithm> 03.using namespace std; 04. 05.bool cmp(int a, ...

  6. python 迭代器和生成器

    1.迭代器协议是指:对象必须提供一个next方法,执行该方法要么返回迭代中的下一项,要么就引起一个StopIteration异常,以终止迭代 (只能往后走不能往前退)2.可迭代对象:实现了迭代器协议的 ...

  7. StyleCop源码分析

    前言: 由于最近在进行项目配置,配置内容:根据一个结构体,一一对应地配置xml文件(两个) 写一个和此结构体对应的类(只包含属性,字段).反复配置后,觉得太繁琐,因此想到使用程序完成自动配置,网上搜索 ...

  8. ubuntu下配置lamp环境

    安装MySQL sudo apt-get install mysql-server mysql-client 安装php模块 Sudo apt-get install php5 安装Apache2 S ...

  9. CoreLocation框架的使用---定位,求两地距离

    前言: 在iOS开发中,有关导航,周边的开发,必须基于2个框架: Map Kit :用于地图展示 Core Location :用于地理定位   用户隐私的保护 从iOS 6开始,苹果在保护用户隐私方 ...

  10. hadoop 集群部署ganglia 监控服务与nagios 报警服务

      1. 部署ganglia 服务   ganglia 涉及到的组件:     数据监测节点(gmond):这个部件装在需要监测的节点上,用于收集本节点的运行情况,并将这些统计信息传送到gmetad, ...