Leetcode 225 Implement Stack using Queues STL
用两个队列去实现栈,这里我使用了队列数组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的更多相关文章
- LeetCode 225 Implement Stack using Queues(用队列来实现栈)(*)
翻译 用队列来实现栈的例如以下操作. push(x) -- 将元素x加入进栈 pop() -- 从栈顶移除元素 top() -- 返回栈顶元素 empty() -- 返回栈是否为空 注意: 你必须使用 ...
- [LeetCode] 225. Implement Stack using Queues 用队列来实现栈
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- 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 ...
- (easy)LeetCode 225.Implement Stack using Queues
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- Leetcode 225 Implement Stack using Queues
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- Java [Leetcode 225]Implement Stack using Queues
题目描述: Implement the following operations of a stack using queues. push(x) -- Push element x onto sta ...
- LeetCode 225 Implement Stack using Queues 用队列实现栈
1.两个队列实现,始终保持一个队列为空即可 class MyStack { public: /** Initialize your data structure here. */ MyStack() ...
- 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 ...
- 232. Implement Queue using Stacks,225. Implement Stack using Queues
232. Implement Queue using Stacks Total Accepted: 27024 Total Submissions: 79793 Difficulty: Easy Im ...
随机推荐
- timingFunction
* 动画的开始与结束的快慢,有五个预置分别为(下同): * kCAMediaTimingFunctionLinear 线性,即匀速 * kCAMediaTimingFunc ...
- Python3 多线程下载代码
根据http://www.oschina.net/code/snippet_70229_2407修改而来的增强版.貌似原版源自Axel这个多线程下载工具. ''' Created on 2014-10 ...
- SAP 常用函数
1. 访问本地 或别的服务器上文件 函数 CALL METHOD CL_GUI_FRONTEND_SERVICES=>EXECUTE EXPORTING DOCUME ...
- vs2013专业版密钥
KCQWK-Q43V3-M3F2T-83VGV-Y6VTX
- 常用UML模型简要小结
关系: 关联(组合,生命周期相同:聚合,物以类聚),依赖,泛化(继承),实现 还有 包含,细化复用已有用例:扩展,非必要主要的用例 图: 1.用例图:就是描述一个功能场景(集合),其实用例编写(前后置 ...
- python多进程
一.多进程池 from multiprocessing import Pool import time pool = Pool(processes=3) result=[];lr=range(t);a ...
- ln 软链接与硬链接的区别再次回顾
以下是整理的笔记 软硬链接区别 硬链接 软链接 文件有相同的 inode 及 data block 是另一个文件 只能对已存在的文件进行创建 可以对不存在的文件进行创建 不能交叉文件系统进行硬链接的创 ...
- JS倒计时网页自动跳转代码
<title>JS倒计时网页自动跳转代码</title> <script language="JavaScript" type="text/ ...
- redhat 5 中文乱码及中文输入法解决方法
安装redhat时中文显示乱码(小方框)解决方法 在安装linux的时候,安装完了中文出现乱码或者是当时选错了选成了英文的,到时候中文显示乱码,下面说一下问题的解决: 在首次安装RHEL5时,如果选择 ...
- ios调用第三方程序打开文件,以及第三方调用自己的APP打开文件
1.自己的APP调用第三方打开文件 主要是使用 UIDocumentInteractionController 类 并实现 UIDocumentInteractionControllerDel ...