https://leetcode.com/problems/implement-stack-using-queues/

还有种方法,就是利用同一个队列,知道队列长度前提下,把内容从头到尾,再向尾部依次重推一下。

package com.company;

import java.util.Deque;
import java.util.LinkedList;
import java.util.Queue; class Solution {
Queue[] queues;
int cur;
Solution() {
queues = new LinkedList[];
queues[] = new LinkedList<Integer>();
queues[] = new LinkedList<Integer>();
cur = ;
} // Push element x onto stack.
public void push(int x) {
queues[cur].offer(x);
} // Removes the element on top of the stack.
public void pop() {
change(true);
} // Get the top element.
public int top() {
return change(false);
} private int change(boolean pop) {
int next = (cur + ) % ;
int num = (int)queues[cur].poll();
while (!queues[cur].isEmpty()) {
queues[next].offer(num);
num = (int)queues[cur].poll();
} if (!pop) {
queues[next].offer(num);
}
cur = next;
return num;
} // Return whether the stack is empty.
public boolean empty() {
return queues[cur].isEmpty();
}
} public class Main { public static void main(String[] args) throws InterruptedException { System.out.println("Hello!");
Solution solution = new Solution(); // Your Codec object will be instantiated and called as such:
solution.push();
solution.push();
solution.push();
int ret = solution.top();
System.out.printf("ret:%d\n", ret);
solution.pop();
ret = solution.top();
System.out.printf("ret:%d\n", ret);
solution.pop();
ret = solution.top();
System.out.printf("ret:%d\n", ret); System.out.println(); } }

implement-stack-using-queues(easy,但也有思考价值)的更多相关文章

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

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

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

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

  3. 【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( ...

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

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

  6. (easy)LeetCode 225.Implement Stack using Queues

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

  7. Implement Stack using Queues 用队列实现栈

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

  8. LeetCode225 Implement Stack using Queues

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

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

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

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

随机推荐

  1. 拼多多2018校招编程题汇总 Python实现

    题目原址 列表补全 在商城的某个位置有一个商品列表,该列表是由L1.L2两个子列表拼接而成.当用户浏览并翻页时,需要从列表L1.L2中获取商品进行展示.展示规则如下: 用户可以进行多次翻页,用offs ...

  2. 解决Win10 中打开VS2012 出现“ASP.NET 4.0 尚未在 Web 服务器上注册”

    系统升级为win10后,在使用vs2012打开原来的项目时,会出现“ASP.NET 4.0 尚未在 Web 服务器上注册”的问题,如图: 想到在win8.1系统下,也出现过同样的问题,就直接使用命令提 ...

  3. [NOI2012][bzoj2879] 美食节 [费用流+动态加边]

    题面 传送门 思路 先看看这道题 修车 仔细理解一下,这两道题是不是一样的? 这道题的不同之处 但是有一个区别:本题中每一种车有多个需求,但是这个好办,连边的时候容量涨成$p\lbrack i\rbr ...

  4. [luoguP1975] [国家集训队]排队(分块)

    传送门 直接暴力分块,然后在每一个块内排序. 查询时可以在每一个块内二分. #include <cmath> #include <cstdio> #include <io ...

  5. Linux下调试段错误 (gdb,core,ulimit)

    Linux环境下经常遇到某个进程挂掉而找不到原因,我们可以通过生成core file文件加上gdb来定位. (1)首先 在makefile中要增加编译调试选项 -g,才可以利用下面的gdb来调试 gc ...

  6. iOS笔记-(缓存机制的理解与实现)

    (1)运行中的现象: 在iOS开发中,会遇到:同一NSURL被多次请求,会造成用户的流量浪费,程序的响应速度不够快.比如说,从服务器上请求一张图片,请求100次,下载的结果都是一样的. (2)解决方法 ...

  7. c++ 中 define

    1.简单的define定义 #define MAXTIME 1000 一个简单的MAXTIME就定义好了,它代表1000,如果在程序里面写 if(i<MAXTIME){.........} 编译 ...

  8. EclEmma Java Code Coverage for Eclipse

      1. 1EclEmma的介绍 一.EclEmma 简介: EclEmma是一个开源的软件测试工具(for eclipse),可以在编码过程中查看代码调用情况.也可以检测单覆盖率. 详见: http ...

  9. FOJ Problem 2273 Triangles

    Problem 2273 Triangles Accept: 201    Submit: 661Time Limit: 1000 mSec    Memory Limit : 262144 KB P ...

  10. URL重写IIS7(URL Rewrite Module) 比之前的urlrewrite更方便使用

    原文发布时间为:2011-02-24 -- 来源于本人的百度文章 [由搬家工具导入] 微软在IIS7中添加了URL的重写模块,并且免费使用,可以导入.htaccess规则,确实是个不错的选择 URL ...