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. java——多线程的实现

    package test; class TestThread extends Thread{ public void run() { for(int n=0;n<3;n++) { try{Thr ...

  2. jsp页面的el表达式取数据

    在jsp页面去Id时候要照上面的方式取,不能照下面的方式取:

  3. android 编译 app

    有些编写的app需要放到android的源码中进行编译.放置的路径packages/apps/ 编译方法,参考 http://blog.csdn.net/luoshengyang/article/de ...

  4. mysql命令行远程登录命令

    mysql -u root -psalon365365 -h 192.168.1.103 -P 3 306 -D empirecms

  5. atitit.javascript调用java in swt attilax 总结

    atitit.javascript调用java in swt attilax 总结 1. BrowserFunction 简单介绍1 1.1. BrowserFunction 能够分为三类: 1 1. ...

  6. 【从0開始Tornado建站】整体设计

    Tornado是一个非堵塞的webserver,也是python的web框架中很优秀的一款.网上关于django的tutorial许多并且具体,关于tornado的使用就很少了.我想以我从0開始的方式 ...

  7. MVC下载远程文件流(WebClient)

    public ActionResult DownLoad_File() { return File(ScLiu(PathUrl), "application/octet-stream&quo ...

  8. PHP curl_setopt函数用法介绍补充篇

    1.curl数据采集系列之单页面采集函数get_html 单页面采集在数据采集过程中是最常用的一个功能 有时在服务器访问限制的情况下 只能使用这种采集方式 慢 但是可以简单的控制 所以写好一个常用的c ...

  9. c++ double float 数值比较

    浮点数在内存中的存储机制和整型数不同,其有舍入误差,在计算机中用近似表示任意某个实数.具体的说,这个实数由一个整数或定点数(即尾数)乘以某个基数(计算机中通常是2)的整数次幂得到,这种表示方法类似于基 ...

  10. 如何通过XAMPP来实现单个服务器上建多个网站

    xampp 是一个非常方便的本地 apache + php + mysql 的调试环境,在本地安装测试 WordPress 等各种博客.论坛程序非常方便.今天我们来给大家介绍一下,如何使用 XAMPP ...