题目:

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. antlr v4 使用指南连载4——词法规则入门之黄金定律

    词法规则入门 黄金定律一二 若输入串能被多个词法规则匹配,那么声明在词法文件最前面的规则生效. parser parser grammar HelloParser; options { languag ...

  2. Java Math的 floor,round和ceil

    floor 返回不大于的最大整数 round 则是4舍5入的计算,入的时候是到大于它的整数 round方法,它表示"四舍五入",算法为Math.floor(x+0.5),即将原来的 ...

  3. 保证service存活

    Android开发的过程中,每次调用startService(Intent)的时候,都会调用该Service对象的onStartCommand(Intent,int,int)方法,然后在onStart ...

  4. Beanstalkd 一个高性能分布式内存队列系统

    需要一个分布式内存队列,能支持这些特性:任务不重不漏的分发给消费者(最基础的).分布式多点部署.任务持久化.批量处理.错误重试..... 转载:http://rdc.taobao.com/blog/c ...

  5. TOMCAT数据源连接配置

    /* *本文档简单介绍系统使用TOMCAT6.0数据源方式连接数据库的配置方法: *1,系统环境:  gdczsam4.0 + Tomcat6.0 + JDK1.5 + SQL Server2008 ...

  6. 【Android 系统开发】 编译 Android文件系统 u-boot 内核 并烧写到 OK-6410A 开发板上

    博客地址 : http://blog.csdn.net/shulianghan/article/details/40299813  本篇文章中用到的工具源码下载 : -- ok-6410A 附带的 A ...

  7. Java数组的应用2:数组的最大,最小,求和,平均值,倒置

    import java.util.Scanner; public class HelloWorld { public static void main(String[] args){ // Scann ...

  8. Linux 进程调度小结

    概述 这个问题又是面试常问问题,当时听到感觉太宽泛了,有点大,心里知道但是说不全,这里做一下总结 [1]进程调度的作用 [2]调度德策略 1. 进程调度的作用 ,进程调度就是对进程进行调度,即负责选择 ...

  9. Linux下修改Oracle数据库字符集命令

    常见情形:从服务器备份Oracle数据库后再到本地机器上还原Oracle数据库的时候经常会碰见数据库字符编码不一致的情况,可以用以下命令来修改本地的Oracle数据库字符编码,然后顺利还原Oracle ...

  10. DesignModeler&nbsp;GestureRecgin…

    DesignModeler : 设计模式     GestureRecginzer:手势识别 作者:韩俊强 原创版权地址:http://blog.sina.com.cn/s/blog_814ecfa9 ...