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 ...
随机推荐
- sd_cms置顶新闻,背景颜色突击显示
维护之前的一个客户网站,使用的是sd_cms系统,因为好久没有维护了,看到这网站的时候,真不敢相信,自己也曾做出过这样的网站. 客户要求置顶新闻始终在最上面,有背景颜色突击显示. 找到对应的代码,修改 ...
- POJ 1065 Wooden Sticks Greed,DP
排序后贪心或根据第二关键字找最长下降子序列 #pragma comment(linker, "/STACK:1024000000,1024000000") #include< ...
- qt显示视频
针对qt4的(视频格式为rgb32) v4l_grab_movie(&v4l_dev); unsigned char *pBuffer= v4l_dev.buffer; QIm ...
- Mac上的抓包工具Charles
http://blog.csdn.net/jiangwei0910410003/article/details/41620363 $********************************** ...
- [vb.net]XML File Parsing in VB.NET
Introduction Parsing XML files has always been time consuming and sometimes tricky. .NET framework p ...
- 解决Failed to load class "org.slf4j.impl.StaticLoggerBinder"
Hibernate使用SLF4J API记录日志,所以在Hibernate的lib中,不再提供Log4J的包,而大部分框架依然使用Log4J记录日志,这样导致了兼容性问题. 解决办法,两步: 一.在编 ...
- HTML5的touch事件
HTML5中新添加了很多事件,但是由于他们的兼容问题不是很理想,应用实战性不是太强,所以在这里基本省略,咱们只分享应用广泛兼容不错的事件,日后随着兼容情况提升以后再陆续添加分享.今天为大家介绍的事件主 ...
- 斯坦福第十三课:聚类(Clustering)
13.1 无监督学习:简介 13.2 K-均值算法 13.3 优化目标 13.4 随机初始化 13.5 选择聚类数 13.1 无监督学习:简介 在这个视频中,我将开始介绍聚类算法.这将是一个 ...
- android中掩码的使用
掩码是一串二进制代码对目标字段进行位与运算,屏蔽当前的输入位,所以有时又称为屏蔽码. 在Android中常使用这种技巧设置flag来判断标记,具体实现可参考framework层的WindowManag ...
- android --- Afianl框架里面的FinalBitmap加载网络图片
Afinal里面FinalBitmap:用于显示bitmap图片,而无需考虑线程并发和oom等问题. 1.测试请求 使用网页打开http://avatar.csdn.net/C/6/8/1_bz419 ...