class Queue {
public:
// Push element x to the back of queue.
void push(int x) {
while (!nums.empty()) {
nums_assist.push(nums.top());
nums.pop();
}
nums.push(x);
while (!nums_assist.empty()) {
nums.push(nums_assist.top());
nums_assist.pop();
}
} // Removes the element from in front of queue.
void pop(void) {
nums.pop();
} // Get the front element.
int peek(void) {
return nums.top();
} // Return whether the queue is empty.
bool empty(void) {
return nums.empty();
}
private:
stack<int> nums, nums_assist;
};

【LeetCode 232_数据结构_队列_实现】Implement Queue using Stacks的更多相关文章

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

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

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

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

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

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

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

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

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

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

  8. [LeetCode] Implement Queue using Stacks 用栈来实现队列

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

  9. LeetCode 225:用队列实现栈 Implement Stack using Queues

    题目: 使用队列实现栈的下列操作: push(x) -- 元素 x 入栈 pop() -- 移除栈顶元素 top() -- 获取栈顶元素 empty() -- 返回栈是否为空 Implement th ...

  10. LeetCode 232:用栈实现队列 Implement Queue using Stacks

    题目: 使用栈实现队列的下列操作: push(x) -- 将一个元素放入队列的尾部. pop() -- 从队列首部移除元素. peek() -- 返回队列首部的元素. empty() -- 返回队列是 ...

随机推荐

  1. POJ3070:Fibonacci(矩阵快速幂模板题)

    http://poj.org/problem?id=3070 #include <iostream> #include <string.h> #include <stdl ...

  2. redhat 5的中文包安装

    中文包文件名.在iso文件的/server/文件夹下fonts-chinese-3.02-9.6.el5.noarch.rpmfonts-ISO8859-2-75dpi-1.0-17.1.noarch ...

  3. mutex锁住共用线程函数 造成了死锁 ,为什么?

    锁住共用的线程函数,为什么出现了死锁的现象,是真的死锁了吗?为什么勒[清晰早点] [逍遥游]# 一般都是用 EnterCriticalSection 和 LeaveCriticalSection 锁住 ...

  4. hdu1199 线段树

    这题说的是给了 n 个操作. 每个操作会把 [a,b] 之间的球 涂为黑色或者 白色, 然后最后问 最长的连续的白色的 球有多少个,初始的时候全是黑的. 我们将所有的点离散化, 记得离散 a-1, b ...

  5. Python中的MySQL接口:PyMySQL & MySQLdb

    MySQLdb模块只支持MySQL-3.23到5.5之间的版本,只支持Python-2.4到2.7之间的版本 PyMySQL支持 Python3.0以后的版本 PyMySQL https://pypi ...

  6. Java实现使用位图生成真值组合

    摘要: 使用位图生成真值组合. 难度: 初级.   /** * 问题描述: 给定 n 个布尔变量,打印所有真值组合. * 例如, n = 2 时 , 所有真值组合为 (true, false),(tr ...

  7. 在Linux系统下统计当前文件夹下的文件个数、目录个数

    1.统计当前文件夹下文件的个数,包括子文件夹里的 ls -lR|grep "^-"|wc -l 如下图: 2.统计文件夹下目录的个数,包括子文件夹里的 ls -lR|grep &q ...

  8. Python笔记 #11# 统计图定制化

    将数据可视化有许多选择: 图的类型 定制化方式 选择什么样的表现方式通常取决于: 数据 你想表达什么 1.Labels # Basic scatter plot, log scale plt.scat ...

  9. pyDay14

    内容来自廖雪峰的官方网站. 1.map的优点:省代码 + 提高可读性. 2.map 运用示例: >>> def f(x): ... return x * x ... >> ...

  10. linux内核分析第八周-理解进程调度时机跟踪分析进程调度与进程切换的过程

    实验原理: 一.调度时机 不同类型的进程有不同的调度需求 第一种分类:        I/O-bound             频繁的进行I/O            通常会花费很多时间等待I/O操 ...