题目:

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

思路:

  • 这道题的意思是用堆栈(stack)来实现队列(quene)
  • 可以用两个stack,s1和s2来实现,s1用来整体的存储,s2用来缓冲倒叙,首先判断s2是否为空,为空,把s1弹出到s2,否则s2.pop()

代码:

class MyQueue {
    Stack<Integer> s1 = new Stack<>();
    Stack<Integer> s2 = new Stack<>();
    // Push element x to the back of queue.
    public void push(int x) {
        s1.push(x);
    }

    // Removes the element from in front of queue.
    public void pop() {
        if(!s2.isEmpty()){
            s2.pop();
        }else{
            if(s1.isEmpty()){
                return;
            }else{
                while(!s1.isEmpty()){
                    int tmp = s1.pop();
                    s2.push(tmp);
                }
                s2.pop();
            }
        }

    }

    // Get the front element.
    public int peek() {
        if(!s2.isEmpty()){
            return s2.peek();
        }else{
            if(s1.isEmpty()){
                return -1;
            }else{
                while(!s1.isEmpty()){
                    int tmp = s1.pop();
                    s2.push(tmp);
                }
                return s2.peek();
            }
        }

    }

    // Return whether the queue is empty.
    public boolean empty() {
        return(s1.isEmpty() && s2.isEmpty());

    }
}

LeetCode(23)-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. leetCode(37):Implement Queue using Stacks

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

  7. Java for LeetCode 232 Implement Queue using Stacks

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

  8. Leetcode 232 Implement Queue using Stacks STL

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

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

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

随机推荐

  1. java中小数的处理:高精度运算用bigDecimal类,精度保留方法,即舍入方式的指定

    一. 计算机的小数计算一定范围内精确,超过范围只能取近似值: 计算机存储的浮点数受存储bit位数影响,只能保证一定范围内精准,超过bit范围的只能取近似值. java中各类型的精度范围参见:http: ...

  2. matlab中的sub2ind函数

    在matlab中,矩阵的存储是按列优先,sub2ind函数将矩阵中指定元素的行列下标转换成存储的序号,即线性索引号.下面,我们举例子进行说明. 1 建立一个3*4*2的矩阵 rng(0,'twiste ...

  3. Win 10 下 android studio显示 Intel haxm无法安装,以及VT-X和hyper-x的冲突问题

               我 的电脑是神舟战神k650c i7 D4,处理器是Intel core i7 4710-MQ,系统是win 10的 我心血来潮想学习一下安卓开发,就首先安装了android s ...

  4. Java采用JDBC的方式连接Hive(SparkSQL)

    前两天,由于系统的架构设计的原因,想通过Java直接访问Hive数据库,对于我这个Java以及Hadoop平台的菜鸟来说,的确是困难重重,不过,还好是搞定了.感觉也不是很麻烦.这篇文章,作为一个感想记 ...

  5. Dynamics CRM 给视图配置安全角色

    CRM2011后给表单设置了安全角色,可以配置实体表单给不同的安全角色查看,但视图的权限始终没有开放配置,这里介绍个工具可以实现这种配置. 先奉上2011/2013版本的工具地址(2015/2016见 ...

  6. Socket编程实践(10) --select的限制与poll的使用

    select的限制 用select实现的并发服务器,能达到的并发数一般受两方面限制: 1)一个进程能打开的最大文件描述符限制.这可以通过调整内核参数.可以通过ulimit -n(number)来调整或 ...

  7. 【Android 应用开发】 FastJson 使用详解

    博客地址 :http://blog.csdn.net/shulianghan/article/details/41011605 fastjson 源码地址 : -- GitHub : https:// ...

  8. 海量数据挖掘MMDS week6: 支持向量机Support-Vector Machines,SVM

    http://blog.csdn.net/pipisorry/article/details/49445387 海量数据挖掘Mining Massive Datasets(MMDs) -Jure Le ...

  9. Socket编程实践(8) --Select-I/O复用

    五种I/O模型介绍 (1)阻塞I/O[默认] 当上层应用App调用recv系统调用时,如果对等方没有发送数据(Linux内核缓冲区中没有数据),上层应用Application1将阻塞;当对等方发送了数 ...

  10. Linux IPC实践(7) --Posix消息队列

    1. 创建/获取一个消息队列 #include <fcntl.h> /* For O_* constants */ #include <sys/stat.h> /* For m ...