翻译

用队列来实现栈的例如以下操作。

push(x) —— 将元素x加入进栈
pop() —— 从栈顶移除元素
top() —— 返回栈顶元素
empty() —— 返回栈是否为空 注意: 你必须使用一个仅仅有标准操作的队列。 也就是说,仅仅有push/pop/size/empty等操作是有效的。 队列可能不被原生支持。这取决于你所用的语言。 仅仅要你仅仅是用queue的标准操作,你能够用list或者deque(double-ended queue)来模拟队列。 你能够如果全部的操作都是有效的(比如,pop或peek操作不会被用在空栈上)。

原文

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 back, peek/pop from front, size, 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).

分析

对栈和队列不清楚的话。能够先看这篇图文介绍:【算法】7 分不清栈和队列?一张图给你完总体会

至于这道题目。有一道很很相似的题目。

本题是用队列来实现栈,以下这题是用栈来实现队列。由于在上一篇中解说过,原理是一样的,大家能够自己看看:LeetCode 232 Implement Queue using Stacks(用栈来实现队列)(*)

代码

class Stack {
public:
queue<int> q, q2; // Push element x onto stack.
void push(int x) {
q.push(x);
} // Removes the element on top of the stack.
void pop() {
if (q.size() == 1) q.pop();
else {
while (q.size() > 1) {
q2.push(q.front());
q.pop();
}
q.pop();
while (q2.size() > 0) {
q.push(q2.front());
q2.pop();
}
}
} // Get the top element.
int top() {
if (q.size() == 1) return q.front();
while (q.size() > 1) {
q2.push(q.front());
q.pop();
}
int temp = q.front();
q2.push(q.front());
q.pop();
while (q2.size() > 0) {
q.push(q2.front());
q2.pop();
}
return temp;
} // Return whether the stack is empty.
bool empty() {
return q.empty();
}
};

LeetCode 225 Implement Stack using Queues(用队列来实现栈)(*)的更多相关文章

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

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

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

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

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

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

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

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

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

  6. Leetcode 225 Implement Stack using Queues

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

  7. Java [Leetcode 225]Implement Stack using Queues

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

  8. Leetcode 225 Implement Stack using Queues STL

    用两个队列去实现栈,这里我使用了队列数组q[2],在所有的过程中保证一个队列是空的 push时插入到空的队列中,然后将队列中的元素移到另一个队列中 pop时从不空的队列中pop() peek时从不空的 ...

  9. 225 Implement Stack using Queues(用队列实现栈Medium)

    题目意思:用队列实现栈,push(),pop(),top(),empty() 思路:用两个queue,pop时将一个queue的元素pop再push到另一个队列,queue只留最后一个元素,并pop, ...

随机推荐

  1. modbus.c

    #include <avr/io.h> #include <avr/interrupt.h> #include <util/delay.h> //#include ...

  2. 深入理解多线程(五)—— Java虚拟机的锁优化技术

    本文是<深入理解多线程>的第五篇文章,前面几篇文章中我们从synchronized的实现原理开始,一直介绍到了Monitor的实现原理. 前情提要 通过前面几篇文章,我们已经知道: 1.同 ...

  3. [转]有关Apache alias的一点问题

    转自:http://www.thinkphp.cn/topic/11973.html Apache 的Alias 指令映射URL到文件系统的特定区域 一个简单的例子: Alias /mytest /w ...

  4. 【翻译自mos文章】Windows平台下的 Oraagent Memory Leak

    来源于: Oraagent Memory Leak (文档 ID 1956840.1) APPLIES TO: Oracle Database - Enterprise Edition - Versi ...

  5. 我讨厌Apple Safari浏览器的一些地方。不想用

    1. 书签栏 无法直接新建文件夹 2. 新建书签 无法新建文件夹 3.地址栏 不显示当前书签 是否已收藏! 4. 书签栏 移动书签,体验没有Chrome好. 5.书签栏 没有chrone的 " ...

  6. Cesium原理篇:3D Tiles(1)渲染调度【转】

    Cesium在2016年3月份左右推出3D Tiles数据规范,在glTF基础上提供了LOD能力,定位就是Web环境下海量三维模型数据.虽然目前3D Tiles还是Beta阶段,有不少硬伤,但3D T ...

  7. WebView&HTML5-----使用WebView播放HTML5视频文件

    一.问题描述 HTML5提供了很多新的特性比如,视频播放.web本地存储.地理定位.应用缓存.Canvas等,但这些特性需要浏览器的支持,在Android应用中我们可以使用WebView构建Web应用 ...

  8. 关于android SDK安装Failed to fetch URL 一点思考

    最近SDK出问题了,然后在google下载了一个android-sdk-windows.rar,然后点击SDK Manager,结果一直不能刷新API Level,然后就开始在网上找了好多资料,解决这 ...

  9. libxml2的安装及使用[总结]

    1.前言 xml广泛应用于网络数据交换,配置文件.Web服务等等.近段时间项目中做一些配置文件,原来是用ini,现在改用xml.xml相对来说可视性更为直观,很容易看出数据之间的层次关系.关于xml的 ...

  10. OpenGL® ES 3.0 Programming Guide - Book Website

    OpenGL® ES 3.0 Programming Guide - Book Website http://opengles-book.com sample codes in GitHub: htt ...