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 ...
随机推荐
- copy & deepcopy
1 import copy 2 3 字典参照列表结论,看是否有深层嵌套. 4 a = {'name':1,'age':2} 5 b = a 6 a['name'] = 'ff' 7 print(a) ...
- Java学习3之成员方法及函数重载
方法的定义:方法名称,返回值,参数列表,修饰符(权限修饰符,final,static),实现体. 参考自:<Java 程序设计与工程实践> 方法的签名: 唯一区别其他方法的元素:(1)方法 ...
- Java之implements
转自:https://blog.csdn.net/android_lover2014/article/details/52176814 JAVA中extends 与implements有啥区别?1. ...
- 【转】UGUI EventSystem
EventSystem The EventSystem is a way of sending events to objects in the application based on inpu ...
- MySql 存储过程实例(附完整注释)(转)
MySql 存储过程实例(附完整注释) 将下面的语句复制粘贴可以一次性执行完,我已经测试过,没有问题! MySql存储过程简单实例: ...
- [HDU3480] Division [四边形不等式dp]
题面: 传送门 思路: 因为集合可以无序选择,所以我们先把输入数据排个序 然后发先可以动归一波 设$dp\left[i\right]\left[j\right]$表示前j个数中分了i个集合,$w\le ...
- C++ qsort() 函数调用时实参与形参不兼容的问题解决
<剑指OFFER>刷题笔记 —— 扑克牌顺子 LL今天心情特别好,因为他去买了一副扑克牌,发现里面居然有2个大王,2个小王(一副牌原本是54张^_^)...他随机从中抽出了5张牌,想测测自 ...
- SELECT中的CAST
SELECT CAST a.b AS int 语法意义 把表别名A的B列的数据类型变为INT
- hdu 5930 GCD 线段树上二分/ 强行合并维护信息
from NOIP2016模拟题28 题目大意 n个点的序列,权值\(<=10^6\) q个操作 1.单点修改 2.求所有区间gcd中,不同数个数 分析 1.以一个点为端点,向左或向右的gcd种 ...
- jquery.transform
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...