一天一道LeetCode

本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github

欢迎大家关注我的新浪微博,我的新浪微博

欢迎转载,转载请注明出处

(一)题目

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)

(二)解题

题目大意:用两个栈实现queue,要求实现push,pop,peek和empty

解题思路:栈是先进后出,队列是先进先出。利用两个栈stack1和stack2

push:和栈一样,直接push到一个栈stack1中

pop:有一个辅助栈就可以将stack1中的数依次push到stack2中,这样stack2的top就是最先进来的数,直接pop即可

peek:去队首的数,如果stack2不为空的话,直接返回stack2.pop即可,如果stack2为空,则先把stack1的数push过来,再去top元素

empty:如果stack1和stack2均为空,就直接返回true,否则返回false

具体实现如下:

class Queue {
public:
    // Push element x to the back of queue.
    void push(int x) {
        st1.push(x);
    }

    // Removes the element from in front of queue.
    void pop(void) {
        if(empty()) return;
        if(st2.empty())
        {
            while(!st1.empty()){//为空的话要把stack1的元素push进来
                st2.push(st1.top());
                st1.pop();
            }
        }
        st2.pop();
    }

    // Get the front element.
    int peek(void) {
        if(st2.empty()){//为空的话要把stack1的元素push进来
            while(!st1.empty()){
                st2.push(st1.top());
                st1.pop();
            }
        }
        return st2.top();
    }

    // Return whether the queue is empty.
    bool empty(void) {
        if(st1.empty()&&st2.empty()) return true;
        else return false;
    }
private:
    stack<int> st1;
    stack<int> st2;
};

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

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

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

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

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

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

  4. Java [Leetcode 232]Implement Queue using Stacks

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

  5. LeetCode 232 Implement Queue using Stacks

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

  6. Java for LeetCode 232 Implement Queue using Stacks

    Stack<Integer> stack=new Stack<Integer>(); public void push(int x) { stack.push(x); } // ...

  7. Leetcode 232 Implement Queue using Stacks STL

    本题用两个栈实现队列,用栈的基本操作去实现队列的所有基本操作push(),pop(),peek()以及empty() sa作为输入栈,sb作为输出栈,将sa输入元素的反转过来放到sb中 push与sa ...

  8. LeetCode 232 Implement Queue using Stacks 两个栈实现队列

    class MyQueue { public: /** Initialize your data structure here. */ MyQueue() { } /** Push element x ...

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

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

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

随机推荐

  1. django rest-framework 1.序列化 一

    上图为项目完整的目录结构 一.入门 需要先安装需要使用到包 pip install django pip install djangorestframework 先来创建一个新项目 django-ad ...

  2. Java实现word文档在线预览,读取office文件

    想要实现word或者其他office文件的在线预览,大部分都是用的两种方式,一种是使用openoffice转换之后再通过其他插件预览,还有一种方式就是通过POI读取内容然后预览. 一.使用openof ...

  3. jvm(三):对象

    关于对象,我们需要面对的问题主要有对象的创建,对象在内存中的布局,对象的结构,对象的访问定位. 对象的创建 对象的创建过程如下图所示: 其主要步骤有:给对象分配内存,初始化对象,执行构造方法. 在对象 ...

  4. 消息中间件--ActiveMQ&JMS消息服务

    ### 消息中间件 ### ---------- **消息中间件** 1. 消息中间件的概述 2. 消息中间件的应用场景 * 异步处理 * 应用解耦 * 流量削峰 * 消息通信   --------- ...

  5. Java锁Synchronized对象锁和类锁区别

    java的内置锁:每个java对象都可以用做一个实现同步的锁,这些锁成为内置锁.线程进入同步代码块或方法的时候会自动获得该锁,在退出同步代码块或方法时会释放该锁.获得内置锁的唯一途径就是进入这个锁的保 ...

  6. 对闭包的理解(closure)

    什么是闭包: 当你声明一个局部变量时,这个局部变量有作用域,通常局部变量值只存在于你定义的Block or Function中: function() { var a = 1; console.log ...

  7. Kali安装使用文泉驿字体

    安装文泉驿字体 Kali选择"简体中文"安装后,在终端等地方发现字体总有重叠,只要安装中文字体即可,这里推荐文泉驿字体.文泉驿是一个以开发开源.免费中文电子资源--如汉字字体.词库 ...

  8. centos 7 破解密码

    CentOS 7 root密码的重置方式和CentOS 6完全不一样,CentOS 7与之前的版本6变化还是比较大的,以进入单用户模式修改root密码为例. 1.重启开机按esc   2.按e     ...

  9. LintCode题解之比较字符串

    使用标记的方式,先遍历一遍B,出现一次就记录一次出现次数,然后遍历A,将记录的B的出现次数消去,最后检查一下记录的标记位是不是都消去了,总共需要检查三次,即进行三次O(n)的遍历. 然后总结出规律如果 ...

  10. Linux下的有用命令

    在之前的博客<Linux下常用命令与使用技巧>中,介绍了Linux的常用命令,在今天的博客中,给大家介绍其他的有用命令. 1.文本转换命令 在Linux下工作,我们不可避免地要和文件格式做 ...