题目:

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 toppeek/pop from topsize, 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).

提示:

此题要求用“栈”实现“队列”,如果单独用一个栈的话,是不可能的。这里的方法是设置两个栈,具体的思路可以看下面代码的注释。

代码:

class Queue {
private:
// 设置两个栈
stack<int> in, out;
public:
/** Push element x to the back of queue.
* 每次push都push到in这个栈当中
*/
void push(int x) {
in.push(x);
} /** Removes the element from in front of queue.
* 经过peek操作后,可以保证out的顶部存放的是队列的队首。
*/
void pop(void) {
peek();
out.pop();
} /** Get the front element.
* 先检查一下out是否为空,是的话就把in里面的数据“倒进”out,
* 这样一来,out的顶部存放的其实是In的底部的数据,也就是最早push进来的数据。
*/
int peek(void) {
if (out.empty()) {
while(in.size()) {
out.push(in.top());
in.pop();
}
}
return out.top();
} /** Return whether the queue is empty.
* in和out都空了才算是没有数据了
*/
bool empty(void) {
return in.empty() && out.empty();
}
};

【LeetCode】232. Implement Queue using Stacks的更多相关文章

  1. 【LeetCode】232. Implement Queue using Stacks 解题报告(Python & Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Python解法 Java解法 日期 [LeetCo ...

  2. 【一天一道LeetCode】#232. Implement Queue using Stacks

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

  3. 【leetcode❤python】232. Implement Queue using Stacks

    #-*- coding: UTF-8 -*-#双栈法class Queue(object):    def __init__(self):        """      ...

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

  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. Leetcode 232 Implement Queue using Stacks 和 231 Power of Two

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

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

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

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

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

  9. (easy)LeetCode 232.Implement Queue using Stacks

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

随机推荐

  1. TCP流量控制和拥塞控制

    TCP的流量控制      所谓的流量控制就是让发送方的发送速率不要太快,让接收方来得及接受.利用滑动窗口机制可以很方便的在TCP连接上实现对发送方的流量控制.TCP的窗口单位是字节,不是报文段,发送 ...

  2. React入门---属性(state)-7

    state------>虚拟dom------>dom 这个过程是自动的,不需要触发其他事件来调用它. state中文理解:页面状态的的一个值,可以存储很多东西. 学习state的使用: ...

  3. gitignore.io-程序猿值得拥有的智能生成gitignore文件的秘密武器

    gitignore.io Create useful .gitignore files for your project by selecting from 360 Operating System, ...

  4. 【JS中循环嵌套常见的六大经典例题+六大图形题,你知道哪几个?】

    首先,了解一下循环嵌套的特点:外层循环转一次,内层循环转一圈. 在上一篇随笔中详细介绍了JS中的分支结构和循环结构,我们来简单的回顾一下For循环结构: 1.for循环有三个表达式,分别为: ①定义循 ...

  5. JSP中include指令和include动作区别

    首先 <%@ include file=” ”%>:为指令元素 <jsp:include page=” ” flush=”true”/>:为 动作元素 先说指令元素: incl ...

  6. qt添加资源文件方法

    File->new file->file and classes->Qt->qt resources->   add name   add->add prefix- ...

  7. javaWeb学习总结(8)- JSP基础语法(2)

    任何语言都有自己的语法,JAVA中有,JSP虽然是在JAVA上的一种应用,但是依然有其自己扩充的语法,而且在JSP中,所有的JAVA语句都可以使用. 一.JSP模版元素 JSP页面中的HTML内容称之 ...

  8. PHP编码规范建议学习

    ###php编码规范 -------* sql过长 ```$sql = <<<SQLSELECT delivery_idFROM d_testWHERE delivery_idIN ...

  9. Spring+SpringMVC+MyBatis深入学习及搭建(五)——动态sql

    转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/6908763.html 前面有讲到Spring+SpringMVC+MyBatis深入学习及搭建(四)——My ...

  10. Python教程(2.2)——数据类型与变量

    和C/C++.Java一样,Python也有数据类型和变量两个概念. 数据类型 Python中的几个基本数据类型为整数(integer/int).浮点数(float/float).布尔值(boolea ...