题目:

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. 2.6、Android Studio创建可伸缩的图片(9-patch文件)

    Draw 9-patch工具是一个所见即所得编辑器,允许你创建可以自动改变大小来适应视图的内容和屏幕的大小. 以下是使用Draw 9-path工具快速创建一个NinePatch图片. 1. 在命令行中 ...

  2. EBS系统克隆

     术语 克隆是对已有的Oracle应用系统创建一份拷贝的过程.克隆一个Oracle应用系统有几种不同的情况,包括: l   标准克隆 – 复制一个已有的Oracle应用系统生成一份拷贝,例如对生产 ...

  3. Android四大组件之一Service介绍-android学习之旅(十二)

    基本概念: service是android四大组件之一,运行在后台执行耗时操作,并不提供用户界面.其他组件如acticity可以通过startService启动该组件,也可以通过bindService ...

  4. Oracle启用scott用户

    先查询一下目前数据库是否有scott用户 select username,account_status from dba_users where username like '%SCOTT%'; 如果 ...

  5. 11 PopupMenu菜单和代码例子

    PopupMenu 弹出式菜单 API 11以上可用 1. 获取弹出菜单的对象 2. 在res里的menu添加菜单项 3. 将布局里的菜单项 给弹出菜单 4. 进行监听弹出菜单 5. 展示出弹出菜单 ...

  6. NoSQL数据库之Redis数据库:Redis的介绍与安装部署

     NoSQL(NoSQL = Not Only SQL),它指的是非关系型的数据库.随着互联网web2.0网站的兴起,传统的关系数据库在应付web2.0网站,特别是超大规模和高并发的SNS类型的w ...

  7. cocos2d-x 3.0 播放MP4视频

    猴子原创,欢迎转载.转载请注明: 转载自Cocos2D开发网–Cocos2Dev.com,谢谢! 原文地址: http://www.cocos2dev.com/?p=545 很久以前写的一个2dx播放 ...

  8. J2EE学习从菜鸟变大鸟之八 企业级项目开发的思考

    什么是企业级项目开发 "企业级项目".企业级项目开发,Java也是企业级项目开发,这个我们到处说.听,每天被我们挂在嘴边,可是到底什么项目才算是"企业级"?自己 ...

  9. Mybatis执行CachingExecutor(六)

    前面几篇博客我们介绍了Excutor及抽象类BaseExecutor和实现类SimpleExecutor.BatchExecutor和ReuseExecutor: 博客列表: Mybatis执行Exe ...

  10. 记一个SwipeMenuListView侧滑删除错乱的Bug

    做侧滑删除网上有很多方案,比如重写Listview实现滑动的监听,今天说下一个SwipeListView,这个是之前一个朋友在网上开源的一个封装组件,能够适用于多种情况,项目地址:https://gi ...