Implement the following operations of a stack using queues.

  • push(x) -- Push element x onto stack.
  • pop() -- Removes the element on top of the stack.
  • top() -- Get the top element.
  • empty() -- Return whether the stack is empty.

Notes:

  • You must use only standard operations of a queue -- which means only push to backpeek/pop from frontsize, and is empty operations are valid.
  • Depending on your language, queue may not be supported natively. You may simulate a queue by using a list or deque (double-ended queue), as long as you use only standard operations of a queue.
  • You may assume that all operations are valid (for example, no pop or top operations will be called on an empty stack)

类似上一题的两个栈实现队列,但是有点不同,具体的见下面的注释:

 class Stack {
public:
// Push element x onto stack.
void push(int x) {
q1.push(x);
} // Removes the element on top of the stack.
void pop() {
int tmp = q1.front();//注意这里的实现和双栈实现队列的方式是不相同的
q1.pop();
while(!q1.empty()){
q2.push(tmp);
tmp = q1.front();
q1.pop();
}
swap(q1,q2);
} // Get the top element.
int top() {
int tmp = q1.front();
q1.pop();
while(!q1.empty()){
q2.push(tmp);
tmp = q1.front();
q1.pop();
}
q2.push(tmp);
swap(q1, q2);//用swap即可,无须再向上次那样再倒回去
return tmp;
} // Return whether the stack is empty.
bool empty() {
return q1.empty();
}
private:
queue<int> q1;
queue<int> q2;
};

LeetCode OJ:Implement Stack using Queues(队列实现栈)的更多相关文章

  1. 225 Implement Stack using Queues 队列实现栈

    Implement the following operations of a stack using queues. push(x) -- Push element x onto stack.pop ...

  2. LeetCode 225 Implement Stack using Queues(用队列来实现栈)(*)

    翻译 用队列来实现栈的例如以下操作. push(x) -- 将元素x加入进栈 pop() -- 从栈顶移除元素 top() -- 返回栈顶元素 empty() -- 返回栈是否为空 注意: 你必须使用 ...

  3. leetcode:Implement Stack using Queues 与 Implement Queue using Stacks

    一.Implement Stack using Queues Implement the following operations of a stack using queues. push(x) - ...

  4. [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 用队列实现栈

    1.两个队列实现,始终保持一个队列为空即可 class MyStack { public: /** Initialize your data structure here. */ MyStack() ...

  6. (easy)LeetCode 225.Implement Stack using Queues

    Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...

  7. LeetCode(36)- Implement Stack using Queues

    题目: Implement the following operations of a stack using queues. push(x) -- Push element x onto stack ...

  8. 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 ...

  9. Leetcode 225 Implement Stack using Queues

    Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...

  10. Java [Leetcode 225]Implement Stack using Queues

    题目描述: Implement the following operations of a stack using queues. push(x) -- Push element x onto sta ...

随机推荐

  1. 这几天添加ccbi 出现的问题

    父类是一个ccbi...在父类的onNodeLoaded 里面添加子类的ccbi ... 出现了父类为空的情况...获取不到时间轴..动画为空... 需要在父类的onEnter里面写添加子类的ccbi ...

  2. DbEntry 4.2 建立关系时的一些问题

    创建关系提示输入字符串的格式不正确 使用HasMany Attribute时,需要将属性访问器的set部分修改为private,否则会提示输入字符串的格式不正确 [HasMany(OrderBy = ...

  3. cordova 插件

    1.移动端WebApp开发,如何实现状态栏沉浸式效果? cordova-plugin-fullscreen 2. cordova热更新插件-不发布应用市场动态更新APP源码 https://githu ...

  4. 20145313张雪纯 《Java程序设计》第2周学习总结

    20145313张雪纯 <Java程序设计>第2周学习总结 教材学习内容总结 3.1.1类型 整数:分为short整数(2字节).int整数(4字节).long整数(8字节). 字节:by ...

  5. Python遍历字典dict的几种方法

    #!/usr/bin/python dict={"a":"apple","b":"banana","o&quo ...

  6. Hive常见问题

    1.HQL是否区分大小写 不区分 hive> select AGE from default.studeNT; --不区分大小写,即使是表中字段 2.查看创建表过程 show create ta ...

  7. 内核启动时在挂载ubi文件系统时提示UBIFS error (ubi0:0 pid 1): ubifs_read_superblock: min. I/O unit mismatch

    一.背景 1.1 笔者机器的内核错误信息如下: UBIFS error (ubi0:0 pid 1): ubifs_read_superblock: min. I/O unit mismatch: 2 ...

  8. "ImportError: cannot import name OVSLegacyKernelSwitch"

    My VirtualMachine OS is Ubuntu14.04, Linux. Recently I want to install the mininet in my OS, and I u ...

  9. 8条规则图解JavaScript原型链继承原理

    原形链是JS难点之一,而且很多书都喜欢用一大堆的文字解释给你听什么什么是原型链,就算有图配上讲解,有的图也是点到为止,很难让人不产生疑惑. 我们先来看一段程序,友情提示sublimeText看更爽: ...

  10. go-ipfs入门及介绍

    1.go-ipfs安装 参考: https://mp.weixin.qq.com/s?__biz=MzUwOTE3NjY3Mw==&mid=2247483734&idx=1&s ...