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. SpringMVC 之类型转换Converter详解转载

    SpringMVC之类型转换Converter详解 本文转载 http://www.tuicool.com/articles/uUjaum 1.1     目录 1.1      目录 1.2     ...

  2. HTML文档的经常使用标记

    一.HTML文档中经常使用的标记有文字标记.段落标记.列表标记.超链接标记.图像标记.表格标记.框架标记和多媒体标记,以下对这些经常使用标记进行介绍: 1.文字标记:文字是网页重要的组成部分之中的一个 ...

  3. android矩阵具体解释

    Matrix.中文里叫矩阵,高等数学里有介绍,在图像处理方面,主要是用于平面的缩放.平移.旋转等操作. 在Android里面,Matrix由9个float值构成.是一个3*3的矩阵. 最好记住.例如以 ...

  4. 验证url 地址是否是图片

    由于正则不是很熟悉 所以面对这样的目前只能采取两步走 一 判断url地址是否是正确的http 二判断后缀是否是图片 格式 /驗證URL function validUrl(strUrl){ strUr ...

  5. 【R】提升R代码运算效率的11个实用方法

    低.有许多种方法可以提升你的代码运算效率,但或许你更想了解运算效率能得到多大的提升.本文将介绍几种适用于大数据领域的方法,包括简单的逻辑调整设计.并行处理和Rcpp的运用,利用这些方法你可以轻松地处理 ...

  6. ASP.NET四种页面导航方式之比较与选择

    一.超级链接 从一个表单进入另一个表单最简单的方式是使用HTML超级链接控件.在Web表单中,使用超级链接的HTML代码类如: <a href="WebForm2.aspx" ...

  7. 如何对抗、预防 SQL注入 攻击

    一.SQL注入简介 SQL注入是比较常见的网络攻击方式之一,它不是利用操作系统的BUG来实现攻击,而是针对程序员编程时的疏忽,通过SQL语句,实现无帐号登录,甚至篡改数据库. 二.SQL注入攻击的总体 ...

  8. 关于Unity中的NGUI和UGUI

    一.用Unity开发2D游戏,有三套关系 1.GUI:Unity本身自带的GUI 2.NGUI:以前在Unity中广泛来做2D的,是第三方的包,需要安装 3.UGUI:Unity5.X后(其实是Uni ...

  9. 机器学习理论之SVM

    支持向量机系列 (1) 算法理论理解 http://blog.pluskid.org/?page_id=683 手把手教你实现SVM算法(一) (2) 算法应用 算法应用----python 实现实例 ...

  10. 常用HQL(Hibernate Query Language)查询

    查询一个对象(实体类必须有一个不带参数的构造方法) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 @Test public void test01() ...