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再来一遍。

class MyStack {
public List<Queue<Integer>> queue;
public int cur;
public MyStack() {
queue = new ArrayList<Queue<Integer>>();
queue.add(new LinkedList<Integer>());
queue.add(new LinkedList<Integer>());
cur = 0;
}
// Push element x onto stack.
public void push(int x) {
queue.get(cur).offer(x);
} // Removes the element on top of the stack.
public void pop() {
while(queue.get(cur).size() > 1) {
queue.get(1-cur).offer(queue.get(cur).poll());
}
queue.get(cur).poll();
cur = 1 - cur;
} // Get the top element.
public int top() {
while(queue.get(cur).size() > 1) {
queue.get(1-cur).offer(queue.get(cur).poll());
}
int t = queue.get(cur).poll();
queue.get(1-cur).offer(t);
cur = 1 - cur;
return t;
} // Return whether the stack is empty.
public boolean empty() {
if(queue.get(cur).isEmpty())
return true;
else
return false;
}
}

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. ExtJs Ext.form.field.TextArea+Ckeditor 扩展富文本编辑器

    Ext.define("MyApp.base.BaseTextArea", { extend: "Ext.form.field.TextArea", xtype ...

  2. C/C++捕获段错误,打印出错的具体位置(精确到哪一行)_转

    转自:C/C++捕获段错误,打印出错的具体位置(精确到哪一行) 修订:2013-02-16 其实还可以使用 glibc 的 backtrace_symbols 函数,把栈帧各返回地址里面的数字地址翻译 ...

  3. CMake使用入门

    一.开胃菜 hello目录下的文件结构: ├── CMakeLists.txt ├── hello.c ├── hello.h └── main.c C代码见下节. 最简单的cmake配置文件: pr ...

  4. Entity Framework应用:使用Code First模式管理存储过程

    在EF中使用存储过程和使用视图是很相似的,一般会使用Database对象上的两个方法:SqlQuery和ExecuteSqlCommand.为了从存储过程中读取很多数据行,我们只需要定义一个类,我们会 ...

  5. shell脚本----for循环

      1.方法1 #!/bin/bash for((i=1;i<10;i++)) do echo $i done 保存为for1.sh 直接sh for1.sh 会报错: Syntax error ...

  6. thikphp 简单的接口骨架

    //get id 获取内容,调用xml方法 public function get(){ $id = $_GET['id'];//接收id $User = M('user'); //$val-> ...

  7. ROS 教程之 vision : 用各种摄像头获取图像

    可能有很多人想在ROS下学习视觉,先用摄像头获取图像,再用opencv做相应算法处理,可是ROS下图像的采集可不像平常的read一下那么简单,需要借助外部package的使用.而摄像头即可以用笔记本自 ...

  8. C API 连接MySQL及批量插入

    CMySQLMgr.h: #ifndef _CMYSQLMGR_H_ #define _CMYSQLMGR_H_ #include <iostream> #include "my ...

  9. e636. Listening to All Key Events Before Delivery to Focused Component

    Registering a key event dispatcher with the keyboard focus manager allows you to see all key events ...

  10. The request lifetime scope cannot be created because the HttpContext is not available

    项目中应用了Autofac,在Global轮询处理Job的时候,需要获取现有得Service,而这些Service已经通过Autofac进行了配置,所以理所应当的用下面的代码去获取了. Depende ...