Implement the following operations of a queue using stacks.

  • push(x) – Push element x to the back of queue.
  • pop() – Removes the element from in front of queue.
  • peek() – Get the front element.
  • empty() – Return whether the queue is empty.

Notes:

  • You must use only standard operations of a stack – which means only

    push to top, peek/pop from top, size, and is empty operations are

    valid.
  • Depending on your language, stack may not be supported natively. You

    may simulate a stack by using a list or deque (double-ended queue),

    as long as you use only standard operations of a stack.
  • You may assume that all operations are valid (for example, no pop or

    peek operations will be called on an empty queue).

使用栈来实现一个队列。这个题目之前在《剑指offer》上面见过,没有什么好说的。使用两个栈,一个栈用来保存插入的元素。另外一个栈用来运行pop或top操作,每当运行pop或top操作时检查另外一个栈是否为空,假设为空,将第一栈中的元素所有弹出并插入到第二个栈中。再将第二个栈中的元素弹出就可以。须要注意的是这道题的编程时有一个技巧,能够使用peek来实现pop。这样能够降低反复代码。编程时要能扩展思维,假设因为pop函数的声明再前面就陷入用pop来实现peek的功能的话就会感觉无从下手了。

runtime:0ms

class Queue {
public:
// Push element x to the back of queue.
void push(int x) {
pushStack.push(x);
} // Removes the element from in front of queue.
void pop(void) {
peek();//这里能够使用peek进行两个栈之间元素的转移从而避免反复代码
popStack.pop();
} // Get the front element.
int peek(void) {
if(popStack.empty())
{
while(!pushStack.empty())
{
popStack.push(pushStack.top());
pushStack.pop();
}
}
return popStack.top();
} // Return whether the queue is empty.
bool empty(void) {
return pushStack.empty()&&popStack.empty();
} private:
stack<int> pushStack;//数据被插入到这个栈中
stack<int> popStack;//数据从这个栈中弹出
};

LeetCode232:Implement Queue using Stacks的更多相关文章

  1. LeetCode232 Implement Queue using Stacks Java 题解

    题目: Implement the following operations of a queue using stacks. push(x) -- Push element x to the bac ...

  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. 232. Implement Queue using Stacks,225. Implement Stack using Queues

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

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

  6. Lintcode: Implement Queue by Stacks 解题报告

    Implement Queue by Stacks 原题链接 : http://lintcode.com/zh-cn/problem/implement-queue-by-stacks/# As th ...

  7. Leetcode 232 Implement Queue using Stacks 和 231 Power of Two

    1. 232 Implement Queue using Stacks 1.1 问题描写叙述 使用栈模拟实现队列.模拟实现例如以下操作: push(x). 将元素x放入队尾. pop(). 移除队首元 ...

  8. LeetCode 232. 用栈实现队列(Implement Queue using Stacks) 4

    232. 用栈实现队列 232. Implement Queue using Stacks 题目描述 使用栈实现队列的下列操作: push(x) -- 将一个元素放入队列的尾部. pop() -- 从 ...

  9. [Swift]LeetCode232. 用栈实现队列 | Implement Queue using Stacks

    Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...

随机推荐

  1. JS高程3:Ajax与Comet-进度事件、跨源资源共享

    有以下 6 个进度事件  loadstart:在接收到响应数据的第一个字节时触发.  progress:在接收响应期间持续不断地触发.  error:在请求发生错误时触发.  abort:在因 ...

  2. cocos2dx错误收集

    1.读取ccb文件onNodeLoaded调用两次的问题 不小心把cocosbuilder里的控件的Custom class里填了两次自定义类,如下: 结果在onNodeLoaded时调用了两次,结果 ...

  3. jQuery 实战读书笔记之第六章:事件本质

    理解浏览器事件模型 understandEventModel.html 代码: <!DOCTYPE HTML> <html> <head> <title> ...

  4. 基于ZK的分布式锁实现

    import java.util.concurrent.TimeUnit; import org.apache.curator.framework.CuratorFramework; import o ...

  5. HDU1196 Lowest Bit

    Lowest Bit Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total ...

  6. FXGameEngine

    最初是在 http://jayskills.com/ 这个网站发现的,但一直未找到源码,今天偶然发现其源码位于 https://github.com/eppleton/FXGameEngine 下载, ...

  7. 谈谈对MVC、MVP和MVVM的理解

    刚出来工作的时候维护过一个老系统,该系统是用微软的ASP(Active Server Pages 动态服务器页面)写的.每一个页面都是一个ASP文件,每一个一个ASP文件中又同时包含了HTML.CSS ...

  8. sudo -E的意思

    1.sudo -E -E选项在man page中的解释是: -E The -E (preserve environment) option indicates to the security poli ...

  9. 蓝桥杯 第三届C/C++预赛真题(7) 放棋子(水题)

    今有 6 x 6 的棋盘格.其中某些格子已经预先放好了棋子.现在要再放上去一些,使得:每行每列都正好有3颗棋子.我们希望推算出所有可能的放法.下面的代码就实现了这个功能. 初始数组中,“1”表示放有棋 ...

  10. PrintArea打印,@media screen解决移动web开发的多分辨率问题,@media print设置打印的样式

    PrintArea打印,局部DIV打印插件,依赖JQuery. github:https://github.com/RitsC/PrintArea 当打印时需要临时改变页面布局,可以使用 @media ...