题目:

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).
Update (2015-06-11):
The class name of the Java function had been updated to MyStack instead of Stack.

思路:

  • 题意是用队列数据结构来实现栈,要求只能使用Queue的基本函数,包括poll,isEmprty等
  • 用两个队列pre和next来实现,pop()操作是把一个非空队列复制到另一个空队列,最后一个不复制,每次判断哪个非空
  • push(int x)的实现:先判断哪个队列非空,然后添加x,isEmpty()是两个队列都为空的时候
  • -

代码:

import java.util.LinkedList;
import java.util.Queue;

class MyStack {
    Queue<Integer> pre = new LinkedList<Integer>();
    Queue<Integer> next = new LinkedList<Integer>();
    // Push element x onto stack.
    public void push(int x) {
        if(next != null){
            next.add(x);
        }else{
            pre.add(x);
        }
    }

    // Removes the element on top of the stack.
    public void pop() {
        if(!next.isEmpty()){
            while(!next.isEmpty()){
                int a = next.poll();
                if(!next.isEmpty()){
                    pre.add(a);
                }
            }
        }else{
            while(!pre.isEmpty()){
                int b = pre.poll();
                if(!pre.isEmpty()){
                    next.add(b);
                }
            }
        }

    }

    // Get the top element.
    public int top() {
        if(!next.isEmpty()){
            while(!next.isEmpty()){
                int c = next.poll();
                 pre.add(c);
                if(next.isEmpty()){
                   return c;
                }
            }
        }else{
            while(!pre.isEmpty()){
                int d = pre.poll();
                next.add(d);
                if(pre.isEmpty()){
                    return d;
                }
            }
      }
        return 0;
    }
    public boolean empty() {
        if(next.isEmpty() && pre.isEmpty()){
            return true;
        }else{
            return false;
        }
    }
}

LeetCode(36)- Implement Stack using Queues的更多相关文章

  1. leetcode:Implement Stack using Queues 与 Implement Queue using Stacks

    一.Implement Stack using Queues Implement the following operations of a stack using queues. push(x) - ...

  2. LeetCode 225 Implement Stack using Queues(用队列来实现栈)(*)

    翻译 用队列来实现栈的例如以下操作. push(x) -- 将元素x加入进栈 pop() -- 从栈顶移除元素 top() -- 返回栈顶元素 empty() -- 返回栈是否为空 注意: 你必须使用 ...

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

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

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

  5. (easy)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. LeetCode 225 Implement Stack using Queues 用队列实现栈

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

随机推荐

  1. 剑指offer面试题3 二维数组中的查找(c)

    剑指offer面试题三:

  2. hive的strict模式;where,group by,having,order by同时使用的执行顺序

    主要限制三种情况 (1) 有partition的表查询需要加上where子句,筛选部分数据实现分区裁剪,即不允许全表全分区扫描,防止数据过大 (2) order by 执行时只产生一个reduce,必 ...

  3. 创建银行API

    DECLARE lc_output VARCHAR2(3000); lc_msg_dummy VARCHAR2(3000); lc_return_status VARCHAR2(3000); lc_m ...

  4. 解决uploadify在使用IE内核等浏览器无法使用

    有两种方法: 第一种: SWFUpload Version: 2.2.0 Beta 2 Flash Player Version: current Operating System:Window 7 ...

  5. Spring mvc,uploadifive 文件上传实践(转自:https://segmentfault.com/a/1190000004503262)

     1.前台页面: 引入js和css 全选复制放进笔记 <link type="text/css" rel="stylesheet" href=&quo ...

  6. 【一天一道LeetCode】#99. Recover Binary Search Tree

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

  7. android eclipse写layout文件失效问题解决

    设置 xml 文件的代码提示功能 打开 Eclipse 依次选择 Window > Preferences > Xml > Editor > Content Assist &g ...

  8. spring mvc接收List集合、JUI传JSP List

    JUI页面是这样的 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 <div class=&quo ...

  9. [WinForm]C# .net防止一个程序(WinForm)重复运行的方法。

    最近比较忙,邮件预警系统暂停了没时间去处理,临时处理:直接执行exe文件! 可是问题来了: 我点击了两次,原来几乎在同时执行这个进程,我在程序中有线程时间睡眠2秒一次等待队列,打开进程果然两个MAIL ...

  10. Java 反射之JDK动态代理

    Proxy提供用于创建动态代理类和代理对象的静态方法,它也是所有动态代理类的父类.如果我们在程序中为一个或多个接口动态地生成实现类,就可以使用Proxy来创建动态代理类:如果需要为一个或多个接口动态的 ...