Description:

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

用队列来实现一个栈。

思路:用两个队列来模拟栈的功能,当前队列(cur)是用来存放数据的,另一个队列是用来替换当前队列的,这样就能操作队尾元素了。两个队列来回切换,一个用来保存数据,另一个用来操作队尾元素。

PS.Java里不能用泛型数组啊,只好用线性表来代替了。代码看起来又复杂了一些。要不要用C++和Python再来一遍。

  1. class MyStack {
  2. public List<Queue<Integer>> queue;
  3. public int cur;
  4. public MyStack() {
  5. queue = new ArrayList<Queue<Integer>>();
  6. queue.add(new LinkedList<Integer>());
  7. queue.add(new LinkedList<Integer>());
  8. cur = 0;
  9. }
  10. // Push element x onto stack.
  11. public void push(int x) {
  12. queue.get(cur).offer(x);
  13. }
  14.  
  15. // Removes the element on top of the stack.
  16. public void pop() {
  17. while(queue.get(cur).size() > 1) {
  18. queue.get(1-cur).offer(queue.get(cur).poll());
  19. }
  20. queue.get(cur).poll();
  21. cur = 1 - cur;
  22. }
  23.  
  24. // Get the top element.
  25. public int top() {
  26. while(queue.get(cur).size() > 1) {
  27. queue.get(1-cur).offer(queue.get(cur).poll());
  28. }
  29. int t = queue.get(cur).poll();
  30. queue.get(1-cur).offer(t);
  31. cur = 1 - cur;
  32. return t;
  33. }
  34.  
  35. // Return whether the stack is empty.
  36. public boolean empty() {
  37. if(queue.get(cur).isEmpty())
  38. return true;
  39. else
  40. return false;
  41. }
  42. }

LeetCode——Implement Stack using Queues的更多相关文章

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

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

  2. (leetcode)Implement Stack using Queues

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

  3. LeetCode Implement Stack using Queues (数据结构)

    题意: 用队列来实现栈. 思路: 没有什么捷径,纯粹模拟.但是用一个队列就够了. class Stack { /* // Push element x onto stack. void push(in ...

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

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

  5. 【LeetCode】232 & 225 - Implement Queue using Stacks & Implement Stack using Queues

    232 - Implement Queue using Stacks Implement the following operations of a queue using stacks. push( ...

  6. leetcode 155. Min Stack 、232. Implement Queue using Stacks 、225. Implement Stack using Queues

    155. Min Stack class MinStack { public: /** initialize your data structure here. */ MinStack() { } v ...

  7. lc面试准备:Implement Stack using Queues

    1 题目 Implement the following operations of a stack using queues. push(x) -- Push element x onto stac ...

  8. 232. Implement Queue using Stacks,225. Implement Stack using Queues

    232. Implement Queue using Stacks Total Accepted: 27024 Total Submissions: 79793 Difficulty: Easy Im ...

  9. Implement Queue by Two Stacks & Implement Stack using Queues

    Implement Queue by Two Stacks Implement the following operations of a queue using stacks. push(x) -- ...

随机推荐

  1. Hadoop Balancer源代码解读

    前言 近期在做一些Hadoop运维的相关工作,发现了一个有趣的问题,我们公司的Hadoop集群磁盘占比数值參差不齐,高的接近80%.低的接近40%.并没有充分利用好上面的资源,可是balance的操作 ...

  2. HISTTIMEFORMAT 设置历史命令时间的格式

    echo 'HISTTIMEFORMAT="%F %T `whoami`"  ' >>/etc/bashrc whoami 完了后面要有空格不然会连住和命令 ===== ...

  3. .net垃圾回收

    垃圾回收器(gc)用来在.NET中进行内存管理,特别是它可以恢复正在运行的应用程序需要的内存. .NET运行库采用的方法是垃圾回收器,这是一个程序,其目的是清理内存.方法是所有动态请求的内存都分配到堆 ...

  4. Mac之安装zsh

    1.安装homebrew ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/in ...

  5. 转载:【原译】Erlang构建和匹配二进制数据(Efficiency Guide)

    转自:http://www.cnblogs.com/futuredo/archive/2012/10/19/2727204.html Constructing and matching binarie ...

  6. CentOS系统资源常用命令

    系统: # uname -a   # 查看内核/操作系统/CPU信息 # cat /etc/issue # cat /etc/redhat-release # 查看操作系统版本 # cat /proc ...

  7. 在VS中写js的同学注意了。。。。。。。。。。。。。。。。。。。

    在vs中安装扩展jsdoc就可以实现这个功能

  8. Centos6.8/7.0搭建Git服务http访问方式

    安装Git版本:git 2.10.0 Git访问方式:基于http的基本验证(非SSL) 1. 安装Apache软件: [root@localhost ~]# yum install httpd 设置 ...

  9. Hibernate学习(1):查询demo

    1.数据库(mysql)创建脚本 DROP TABLE IF EXISTS role; CREATE TABLE IF NOT EXISTS `role`( `id` ) NOT NULL AUTO_ ...

  10. informatica中的workflow连接远程数据库

    如果是远程oracle这样写 名称随便起,方便自己记住,后面用户名密码你都知道,再加上数据库的地址:端口/SID就可以了. 如10.33.2.208:1521/torcl