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. iOS9新特性

    本文主要是说一些iOS9适配中出现的坑,如果只是要单纯的了解iOS9新特性可以看瞄神的开发者所需要知道的 iOS 9 SDK 新特性.9月17日凌晨,苹果给用户推送了iOS9正式版,随着有用户陆续升级 ...

  2. Nmap笔记本

    nmap -vv 192.168.1.100 -p1-65535 跑1-65535的端口并且设置对结果的详细输出 nmap -vv 192.168.1.1/8 -p 1433 --open 跑开放14 ...

  3. hadoop单节点配置

    首先按照官网的单机去配置,如果官网不行的话可以参考一下配置,这个是配置成功过的.但是不一定每次都成功 http://hadoop.apache.org/docs/r2.6.5/ centos 6.7 ...

  4. hdu6055 Regular polygon 脑洞几何 给定n个坐标(x,y)。x,y都是整数,求有多少个正多边形。因为点都是整数点,所以只可能是正四边形。

    /** 题目:hdu6055 Regular polygon 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6055 题意:给定n个坐标(x,y).x,y都 ...

  5. SAM I AM UVA - 11419 最小点集覆盖 要输出具体覆盖的行和列。

    /** 题目:SAM I AM UVA - 11419 链接:https://vjudge.net/problem/UVA-11419 题意:给定n*n的矩阵,'X'表示障碍物,'.'表示空格;你有一 ...

  6. linux查看硬盘空间 文件大小

    du,disk usage,是通过搜索文件来计算每个文件的大小然后累加,du能看到的文件只是一些当前存在的,没有被删除的.他计算的大小就是当前他认为存在的所有文件大小的累加和 df,disk free ...

  7. net mvc 小目标

    1.前台视图去找指定的控制器(非默认) 2.控制器去找指定的视图(非默认)

  8. 机器学习之svm---cv wiki svm

    http://www.opencv.org.cn/opencvdoc/2.3.2/html/doc/tutorials/ml/introduction_to_svm/introduction_to_s ...

  9. win7 激活相关

    命令 slui 1 slui 2 slui 3 slui 4 slmgr.vbs 需打开的服务 需要开启software protection和 SPP Notification service这两个 ...

  10. bootstrap随笔点击增加

          ht5:   <div class="form-group"><label class="col-sm-2 control-label&qu ...