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.

不用多想,可以使用两个queue来实现,代码如下

耗时在pop上

class Stack {
public:
// Push element x onto stack.
void push(int x) {
que1.push(x);
} // Removes the element on top of the stack.
void pop() {
while (que1.size() > )
{
que2.push(que1.front());
que1.pop();
}
que1.pop();
while (!que2.empty())
{
que1.push(que2.front());
que2.pop();
}
} // Get the top element.
int top() {
return que1.back();
} // Return whether the stack is empty.
bool empty() {
return que1.empty();
}
private:
queue<int> que1;
queue<int> que2;
};

查看其它人的代码,发现居然可以使用一个queue来实现,实现思路非常巧妙,每一次push操作都将queue之前的元素都移到后面,使其保持栈的排序

如:

push 1

1

push 2

2 1

push 3

3 2 1

class Stack {
public:
queue<int> que;
// Push element x onto stack.
void push(int x) {
que.push(x);
for (int i = ; i<que.size() - ; ++i) {
que.push(que.front());
que.pop();
}
} // Removes the element on top of the stack.
void pop() {
que.pop();
} // Get the top element.
int top() {
return que.front();
} // Return whether the stack is empty.
bool empty() {
return que.empty();
}
};

Implement Stack using Queues leetcode的更多相关文章

  1. Implement Stack using Queues ——LeetCode

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

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

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

  3. 【LeetCode】232 & 225 - Implement Queue using Stacks & Implement Stack using Queues

    232 - Implement Queue using Stacks Implement the following operations of a queue using stacks. push( ...

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

  5. 232. Implement Queue using Stacks,225. Implement Stack using Queues

    232. Implement Queue using Stacks Total Accepted: 27024 Total Submissions: 79793 Difficulty: Easy Im ...

  6. Implement Queue by Two Stacks & Implement Stack using Queues

    Implement Queue by Two Stacks Implement the following operations of a queue using stacks. push(x) -- ...

  7. [LeetCode] Implement Stack using Queues 用队列来实现栈

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

  8. 【一天一道LeetCode】#225. Implement Stack using Queues

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Impleme ...

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

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

随机推荐

  1. poj 1013(uva 608) Counterfeit Dollar

    #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #in ...

  2. Mac 安装Rudy环境 pod安装前的准备工作

    之前已经说过怎么使用pod 今天说一下安装pod之前的准备工作 首先呢就是Rudy 环境(前提是你已经安装了Xcode) 在终端输入一下命令 期间可能也许会要你输入密码 curl -L https:/ ...

  3. eclipse开发velocity实例(初学)

    开发环境         Eclipse Java EE IDE for Web Developers.(Version: Helios Service Release 1) jdk1.6.0_07 ...

  4. Java 字符终端上获取输入三种方式

    http://blog.csdn.net/hongweigg/article/details/14448731 在Java 字符终端上获取输入有三种方式: 1.java.lang.System.in ...

  5. 二维动态规划——Palindrome

    Palindrome Description A palindrome is a symmetrical string, that is, a string read identically from ...

  6. 你真的懂ajax吗?

    前言 总括: 本文讲解了ajax的历史,工作原理以及优缺点,对XMLHttpRequest对象进行了详细的讲解,并使用原生js实现了一个ajax对象以方便日常开始使用. damonare的ajax库: ...

  7. Mac bash rc

    ###################################### ########## .bashrc ###################################### cas ...

  8. POJ2187(旋转卡壳)

    Beauty Contest Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 35459   Accepted: 10978 ...

  9. python之简单主机批量管理工具

    今天做了一个很简单的小项目,感受到paramiko模块的强大. 一.需求 二.简单需求分析及流程图 需求很少,我就简单地说下: 1. 主机分组可以配置文件实现(我用字典存数据的). 2. 登陆功能不做 ...

  10. ubuntu 压缩软件

    7-zip 安装: sudo apt-get install p7zip-full 使用方法: 7z x file file是你要解压的文件名 更多使用方法: man 7z rar sudo apt- ...