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. [转]jQuery DOM Ready

    一直以来,各种JS最佳实践都会告诉我们,将JS放在HTML的最后,即</body>之前,理由就是:JS会阻塞下载,而且,在JS中很有可能有对DOM的操作,放在HTML的最后,可以尽可能的保 ...

  2. CTF 两道web整数溢出题目(猫咪银行和ltshop)

    ①猫咪银行: (2018中科大hackgame) 一开始给十个CTB,而flag需要20个CTB,我们需要理财赚够20个. 理财是只能买入TDSU才可以获得收益.我们先上来直接把CTB全部换成TDSU ...

  3. java线程安全问题原因及解决办法

    1.为什么会出现线程安全问题 计算机系统资源分配的单位为进程,同一个进程中允许多个线程并发执行,并且多个线程会共享进程范围内的资源:例如内存地址.当多个线程并发访问同一个内存地址并且内存地址保存的值是 ...

  4. RHCE认证经典考题

    1:按要求创建用户组及多个用户,设置用户的候选组,设置用户的默认shell. 创建组adminuser. 创建用户natasha和harry属于该组(该组为他们的第二个组). 创建用户sarah,不属 ...

  5. CSU-1985 驱R符

    CSU-1985 驱R符 Description 阴阳师中有三中稀有度的式神R,SR,SSR,其中R的稀有度最低,每次抽符,会随机得到一种式神,然而子浩君对R式神已经深恶痛绝. 某天,子浩君突然发现, ...

  6. maven学习(十四)——Eclipse中使用Maven插件

    1.导入Maven项目 File→import

  7. [HAOI2011][bzoj2301] Problem b [莫比乌斯反演+容斥原理+分块前缀和优化]

    题面: 传送门 有洛谷就尽量放洛谷链接呗,界面友好一点 思路: 和HDU1695比较像,但是这一回有50000组数据,直接莫比乌斯反演慢慢加的话会T 先解决一个前置问题:怎么处理a,c不是1的情况? ...

  8. error: ‘to_string’ was not declared in this scope

    错误: error: ‘to_string’ was not declared in this scope 原因: to_string是C++11引入的新功能,旧版本编译器可能不支持它,所以要给编译器 ...

  9. exit() 与 return() 的区别

    exit()与 return() 的区别为: 1. return返回函数值,是关键字:  exit 是一个函数. 2. return是语言级别的,它表示了调用堆栈的返回:而exit是系统调用级别的,它 ...

  10. shell 条件表达式

    1.条件测试的常用语法如下 1.test 测试表达式 2.[ 测试表达式 ] #两边需要有空格 3.[[ 测试表达式 ]] 4.(( 测试表达式 )) 说明: 第一种和第二种是等价的,第三种是扩展的t ...