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 ...
随机推荐
- HttpModule的一些初步认识
新建一个类 ValidaterHttpModuleEvents继承管道接口 IHttpModule,代码如下 public class ValidaterHttpModuleEvents:IHttpM ...
- javascript 的事件--阻止冒泡
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8&qu ...
- PHP对自己I/O流访问的封装(转)
php://stdin:访问PHP进程相应的输入流,比如用在获取cli执行脚本时的键盘输入. php://stdout:访问PHP进程相应的输出流. php://stderr:访问PHP进程相应的错误 ...
- SQL Server 扩展一个支持类似。net 时间格式化的标量函数~
* FROM sys.objects WHERE name=N'uF_DateFormat' AND [type]='FN') DROP FUNCTION uF_DateFormat GO SET A ...
- js 去掉字符串前后空格
//去左空格; function ltrim(s){ return s.replace(/(^\s*)/g, ""); } //去右空格; function rtrim(s){ r ...
- Java如何将html转以后的字符转化成正常显示的字符
String str = "“!@#¥%……&——+”";//“!@#¥%……&——+” 中文状态下的标点符号,进过html转换了 String convStr ...
- c#开发Mongo笔记第五篇
现在增删查改算是都完成了,但是查询算是有点不完美的,相信现在用juqeryeasyui这一类的插件的人应该也不少吧,这样的话前台展示需要JSON格式的数据, 好在mogno驱动提供toJson()的函 ...
- freeCodeCamp:Title Case a Sentence
确保字符串的每个单词首字母都大写,其余部分小写. 像'the'和'of'这样的连接符同理. /*思路 将字符串转为小写.toLowerCase() 分割字符串以单词形式组成数组myarr 确保数组中的 ...
- jquery实现checkbox的全选
<html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"> < ...
- JavaScript-数据引用类型对象
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...