一天一道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. 简述RIP路由协议和OSPF路由协议的相同点和不同点。

    路由协议分为静态路由协议和动态路由协议.动态路由协议有很多种,如RIP.OSPF.EIGRP等. 1.RIP(路由信息协议)是路由器生产商之间使用的第一个开放标准.RIP有两个版本:RIPv1和RIP ...

  2. HashMap实现原理和源码解析

    哈希表(hash table)也叫散列表,是一种非常重要的数据结构.许多缓存技术(比如memcached)的核心其实就是在内存中维护一张大的哈希表,本文会对java集合框架中的对应实现HashMap的 ...

  3. Mianbot:基于向量匹配的情境式聊天机器人

    Mianbot是采用样板与检索式模型搭建的聊天机器人,目前有两种产生回覆的方式,专案仍在开发中:) 其一(左图)是以词向量进行短语分类,针对分类的目标模组实现特征抽取与记忆回覆功能,以进行多轮对话,匹 ...

  4. Linux下解决无法远程连接数据库问题

    起因 今天在ubuntu16.04环境下通过mysql workbench访问远程数据库时,发现无法连接问题,解决思路及方法记录如下,不足之处,请多指教. 问题 通过workbench输入密码访问时报 ...

  5. IBM-x3650做RAID5更换硬盘后rebuild步骤分享

    1.按Ctrl+H进入WebBIOS配置 2.点击start 3.点击Drives,对slot5进行配置 4.选择slot5,选择Properties,点击Go按钮 5.选择MakeUnconfGoo ...

  6. [ Java学习基础 ] Java的抽象类与接口

    一.抽象类 1. 抽象类 Java语言提供了两种类:一种是具体类:另一种是抽象子类. 2. 抽象类概念: 在面向对象的概念中,所有的对象都是通过类来描绘的,但是反过来,并不是所有的类都是用来描绘对象的 ...

  7. 【问底】徐汉彬:亿级Web系统搭建——单机到分布式集群

    http://www.csdn.net/article/2014-11-06/2822529/3 大规模流量的网站架构,从来都是慢慢"成长"而来.而这个过程中,会遇到很多问题,在不 ...

  8. JVM体系结构-----深入理解内存结构

    一.概述 内存在计算机中占据着至关重要的地位,任何运行时的程序或者数据都需要依靠内存作为存储介质,否则程序将无法正常运行.与C和C++相比,使用Java语言编写的程序并不需要显示的为每一个对象编写对应 ...

  9. 有趣的冷知识:编程中Foo, Bar 到底什么意思?

    转自:编程中Foo, Bar 到底什么意思? 1 前言 在很多国外计算机书本和一些第三份开源软件的Demo中经常用到两个英文单词Foo,Bar.这到底是什么意思呢?从步入屌丝界的IT生活见到这两个单词 ...

  10. Java第8次实验(IO流)

    参考资料 本次作业参考文件 正则表达式参考资料 第1次实验 1. 字符流与文本文件:使用 PrintWriter(写),BufferedReader(读) 参考文件:基础代码目录Student.jav ...