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 ...
随机推荐
- Android之activity中新建控件
了解了5大布局,我们会发现这些布局都是静态的,如何让系统自动生成控件呢?这就需要activity来帮忙了 今天我们讲的就是用activity新建布局 用案例来说吧! 实现一个输入行和列自动生成表格并生 ...
- 请不要做浮躁的IT人
1.不要看到别人的回复第一句话就说:给个代码吧!你应该想想为什么.当你自己想出来再参考别人的提示,你就知道自己和别人思路的差异. 2.初学者请不要看太多太多的书那会误人子弟的,先找本系统的学,很多人用 ...
- python-flask 框架使用 flask_mongoengine
开发环境配置 再使用 mongodb 之前,需要先安装 pymongo ,以及flask_mongoengine 1. 切换到 virtualenv 环境 . /pyenv/bin/activate ...
- 小甲鱼python视频弟十二讲(关于字符串的方法及注释下)
1,ljust(width[, fillchar]) width -- 指定字符串长度. fillchar -- 填充字符,默认为空格. 用法:返回一个原字符串左对齐,并使用空格填充至指定长度的新字 ...
- javascript学习第三课引用类型object
主要内容: 1.object 是所有类型的基类 实例化对象: 1. var obj = new Object(); 2. var obj = {}; 设置对象属性和方法: obj.name = 'he ...
- dropbear
生成ssh连接所需要的公钥,如下: /usr/bin/dropbearkey -t dss -f /etc/dropbear/dropbear_dss_host_key (dss加密,长度默认 ...
- 访问https链接方法
<a id='___szfw_logo___' href='https://credit.szfw.org/CX20160808028375110138.html' target='_blank ...
- 根据UserAgent 获取操作系统名称
/// <summary> /// 根据 User Agent 获取操作系统名称 /// </summary> private sta ...
- java矩阵相乘的计算
package a123; import java.util.Scanner; public class a132 { public static void main(String args[]) { ...
- sublim text3 配置
喜欢用sublime,但每次换环境都要重新百度下配置,太麻烦,故在此把自己喜欢的配置记录下来 sublime text2 可以直接在设置里改,但sublime text3不能直接在设置中改值,只能在s ...