用两个队列去实现栈,这里我使用了队列数组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. Facebook内部分享:26个高效工作的小技巧

    春节假期马上就要结束了,该收收心进入新一年的工作节奏了~分享 26 个高效工作的小技巧,希望对大家有所帮助~(我发现自己只有最后一条执行得很好,并且堪称完美!) 1.时间常有,时间优先. 2.时间总会 ...

  2. 清除浮动的 why

    如果你想第三个p不被前面的浮动层所影响,就对它进行清除如果没有清除,第三个层就会移到第一个p下面 记住!!浮动是用来布局的~你看你的网页设计图,好几个版块在一条线上就是要浮动了,不需要浮动就是版块跟前 ...

  3. 使用Resource Owner Password Credentials Grant授权发放Token

    对应的应用场景是:为自家的网站开发手机 App(非第三方 App),只需用户在 App 上登录,无需用户对 App 所能访问的数据进行授权. 客户端获取Token: public string Get ...

  4. div mouseenter 事件在IE下无效

    解决方法是在div style上加个背景色: background: rgba(0, 0, 0, 0);filter:alpha(opacity=0); background: rgba(0, 0, ...

  5. js中实现字母大小写转换

    js中实现字母大小写转换主要用到了四个js函数: 1.toLocaleUpperCase  2.toUpperCase3.toLocaleLowerCase4.toLowerCase 下面就这四个实现 ...

  6. 解决: Can’t connect to local MySQL server through socket /var/lib/mysql/mysql.sock

    今天在搬瓦工上使用mysql 命令行,总报一个这样的错:Can't connect to local MySQL server through socket '/xxxxx/mysql.sock',一 ...

  7. 完美解决IE8有两个进程的问题

    完美解决IE8有两个进程的问题,照以下方法设置后就只有一个进程了,没有什么负影响哦! 方法: 1.win+R,在运行框里输入:gpedit.msc,回车进入组策略设置. 2.依次展开:计算机配置——管 ...

  8. js注意

    使用集成函数注意返回值,有的不会改变现有对象,仅返回对象的副本,而有的会改变现有对象并返回该对象. 变量名不能和函数名相同,否则会被覆盖. 查询时看清楚返回的是单个元素还是集合,如果是使用返回集合的方 ...

  9. 【转发】Cross-thread operation not valid: Control 'progressBar1' accessed from a thread other than the thread it was created on

    当您试图从单独的线程更新一个win form时,您将得到如下错误信息:  "Cross-thread operation not valid: Control 'progressBar1' ...

  10. angular-ui-bootstrap的进度条问题及解决

    在测试angular-ui-bootstrap中的进度条的时候,用的是官方的示例代码,但是跑不起来. 经过代码比对之后,发现官方用的是0.14.3, 而我本地用的是0.13.3 (2015-08-09 ...