题目:

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. 第一行代码阅读笔记---AndroidMainfest.xml分析

    按照这本书的指引,我随作者一样创建了一个安卓应用,开始了安卓开发的启程. 找到AndroidMainfest.xml这个文件,打开后看到了我创建的Activity在这个文件里被成功注册,文件内容如下: ...

  2. TCP的发送系列 — tcp_sendmsg()的实现(二)

    主要内容:Socket发送函数在TCP层的实现 内核版本:3.15.2 我的博客:http://blog.csdn.net/zhangskd 在上篇blog中分析了tcp_sendmsg()这个主要函 ...

  3. antlr v4 使用指南连载4——词法规则入门之黄金定律

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

  4. Spark 1.0 开发环境构建:maven/sbt/idea

    因为我原来对maven和sbt都不熟悉,因此使用两种方法都编译了一下.下面记录一下编译时候遇到的问题.然后介绍一下如果使用IntelliJ IDEA 13.1构建开发环境. 首先准备java环境和sc ...

  5. 【VSTS 日志】VSTS 所有功能,看这个页面就够了!

    随着Connect();//2015大会的结束,一大波的好消息随之而来.今天小编刚刚发现了Visual Studio Team Services / Team Foundation Server 的完 ...

  6. Linux:alias永久生效

    alias(中文称为"别名")允许使用更加简短的名称来重新定义 Linux 中的 Shell 命令,从而简化命令行的输入. 如果经常与 CLI 打交道,那么使用 alias 不仅会 ...

  7. CentOS配置

    1.在Vmware中安装好虚拟机. 2.客户机(即虚拟机中的centos)网络连接使用仅主机模式Host-only 3.在主机中网络配置上,配置IP地址 1.使用PieTTY远程连接该虚拟机 2.使用 ...

  8. IE浏览器打印的页眉页脚设置解决方法

    首先说明问题: 默认情况下,通过IE的打印对话框,打印出来的内容都有页眉和页脚的. 查看ie的页面设置发现如右图中,页眉页脚 下面先说明&w&bPage&p of &P ...

  9. Dynamics crm2013 IFD部署后启用多组织

    对CRM的多组织都不会陌生,在常规模式下新建组织后更改下url后面的组织名称即可访问新的组织了,而在部署IFD后就需要注意两点:1.域名的DNS解析:2.relying patry trust的更新 ...

  10. Web资源认证原理

    Web服务器与浏览器之间的认证流程没有规定的步骤,根据不同的认证模式及鉴权方式可能会有不同的执行步骤.下图用一个最简单的流程了解整个认证过程是如何工作的,首先浏览器向服务器发起请求,然后服务器向浏览器 ...