implement-stack-using-queues(easy,但也有思考价值)
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,但也有思考价值)的更多相关文章
- 232. Implement Queue using Stacks,225. Implement Stack using Queues
232. Implement Queue using Stacks Total Accepted: 27024 Total Submissions: 79793 Difficulty: Easy Im ...
- leetcode:Implement Stack using Queues 与 Implement Queue using Stacks
一.Implement Stack using Queues Implement the following operations of a stack using queues. push(x) - ...
- 【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( ...
- 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 ...
- 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) -- ...
- (easy)LeetCode 225.Implement Stack using Queues
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- Implement Stack using Queues 用队列实现栈
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- LeetCode225 Implement Stack using Queues
Implement the following operations of a stack using queues. (Easy) push(x) -- Push element x onto st ...
- [LeetCode] Implement Stack using Queues 用队列来实现栈
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- 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 ...
随机推荐
- 最简单的RSA及其几个网站和工具
最简单的形式 给你公钥和一个密文. flag.enc就是密文,我们用记事本是看不出什么的,其实也不用看,因为后边的解密是直接用脚本读取文件的,只需要知道这是密文. pub.pem就是公钥,用记事本打开 ...
- sqlserver执行时间和自行效率
SET STATISTICS PROFILE ON --SET STATISTICS IO ON --SET STATISTICS TIME ON declare @dtm datetime SQL语 ...
- 【转】 [UnityUI]UGUI射线检测
http://blog.csdn.net/lyh916/article/details/50947026 1.Graphic Raycaster 主要用于UI上的射线检测,挂有这个组件的物体,必须要挂 ...
- maven国内镜像
<?xml version="1.0" encoding="UTF-8"?> <!--Licensed to the Apache Softw ...
- mybatis的使用及详解
一.Mybatis介绍 MyBatis是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架.MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索封装.MyBatis可以 ...
- BZOJ 4069 [Apio2015]巴厘岛的雕塑 ——贪心
自己首先想了一种方法$f(i)$表示前$i$个最小值为多少. 然而发现位运算并不满足局部最优性. 然后我们可以从高到低贪心的判断,使得每一组的和在一个特定的范围之内. 还要特判最后一个Subtask, ...
- 几种API接口
实用号码归属地查询(IP 地址,手机号码): 默认格式: http://api.liqwei.com/location/ (使用来访者的 IP 地址) 指定 IP 地址格式: http://api.l ...
- e.keyCode和e.which使用
1. 不使用jquery获取keyCode var key = 'which' in e ? e.which : e.keyCode;//或者var key = e.which || e.keyCod ...
- Codeforces Round #440(Div.2)
一句话题意: A:给出两个长为\(n\),\(m\)的的数组,每个数在\(1\)到\(9\)之间,求出一个最小的数使得至少有一位出现在一个数组中,且至少有一位出现在另一个数组中.\(n,m\leq9\ ...
- python常用内建函数
1.input 读取控制台的输入,输出的是字符串 2.enumerate 遍历数组的时候,能够将index 和item同时返回,返回的每一项包含index,item 3.isinstance(obje ...