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. sql2008查看备份进度

    SELECT session_id, request_id, start_time, status, command, sql_handle --,statement_start_offset, st ...

  2. [python][django学习篇][3]创建django web的数据库模型

    推荐学习博客:http://pythonzh.cn/post/8/ 博客或者web界面向用户展示内容,它需要从某个地方获取博客内容或者web界面内容,才能够展示出来.通常来说:某个地方指的就是数据库 ...

  3. 团队Alpha版本冲刺(二)

    目录 组员情况 组员1(组长):胡绪佩 组员2:胡青元 组员3:庄卉 组员4:家灿 组员5:凯琳 组员6:丹丹 组员7:家伟 组员8:政演 组员9:黄鸿杰 组员10:刘一好 组员11:何宇恒 展示组内 ...

  4. 打开任意位置的webConfig

    请阅读原文:打开任意的配置文件 翻翻ConfigurationManager的签名,有一个方法吸引了我的注意:OpenExeConfiguration(string exePath).看上去我可以把B ...

  5. MySql数据库 - 1.安装

    下载: 官网:www.mysql.com 打开官网之后依次点击:DOWNLOADS - Windows - MySql Installer MySql Installer 包含的功能,使用C#连接数据 ...

  6. javascript原生360 开机小动画

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  7. table中填写数据并批量增加

    <table class = "table jtable table-bordered table-striped hide" id = "table_1" ...

  8. 【线性基】51nod1312 最大异或和&LOJ114 k大异或和

    1312 最大异或和 题目来源: TopCoder 基准时间限制:1 秒 空间限制:131072 KB 分值: 320 难度:7级算法题   有一个正整数数组S,S中有N个元素,这些元素分别是S[0] ...

  9. easyUI 接收Spring Mvc中@ResponseBody中文乱码解决

    接触springMVC不够深入,乱码困扰我到深夜,特此留下记忆: @responsebody默认滴是ISO-8859-1 Controller注解参数 @ResponseBody 标注后返回Strin ...

  10. css 三列布局

    前面的话 前面已经介绍过单列定宽单列自适应和两列自适应的两列布局.本文介绍三列布局,分为两侧定宽中间自适应.两列定宽一侧自适应.中间定宽两侧自适应.一侧定宽两列自适应和三列自适应这五种情况 两侧定宽中 ...