LeetCode--255--用队列实现栈(java版)
使用队列实现栈的下列操作:
- push(x) -- 元素 x 入栈
- pop() -- 移除栈顶元素
- top() -- 获取栈顶元素
- empty() -- 返回栈是否为空
注意:
- 你只能使用队列的基本操作-- 也就是
push to back
,peek/pop from front
,size
, 和is empty
这些操作是合法的。 - 你所使用的语言也许不支持队列。 你可以使用 list 或者 deque(双端队列)来模拟一个队列 , 只要是标准的队列操作即可。
- 你可以假设所有操作都是有效的(例如, 对一个空的栈不会调用 pop 或者 top 操作)。
超级慢的。
class MyStack {
private LinkedList<Integer> list1 ;
private LinkedList<Integer> list2 ;
/** Initialize your data structure here. */
public MyStack() {
this.list1 = new LinkedList<Integer>();
this.list2 = new LinkedList<Integer>();
} /** Push element x onto stack. */
public void push(int x) {
list1.add(x);
} /** Removes the element on top of the stack and returns that element. */
public int pop() {
int res = 0;
if(list1.size()==1){
return list1.poll();
}else{
while(list1.size()!=1){
list2.add(list1.poll());
}
res = list1.poll();
while(!list2.isEmpty()){
list1.add(list2.poll());
}
}
return res;
} /** Get the top element. */
public int top() {
int res = 0;
if(list1.size() == 1){
res = list1.peek(); }else{
while(list1.size()!=1){
list2.add(list1.poll());
}
res = list1.peek();
list2.add(list1.poll());
while(!list2.isEmpty()){
list1.add(list2.poll());
}
}
return res;
} /** Returns whether the stack is empty. */
public boolean empty() {
if(list1.isEmpty() && list2.isEmpty()){
return true;
}else{
return false;
}
}
} /**
* Your MyStack object will be instantiated and called as such:
* MyStack obj = new MyStack();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.top();
* boolean param_4 = obj.empty();
*/
这个比较快:
class MyStack { List<Integer> queue1=new LinkedList();
List<Integer> queue2=new LinkedList();
private int num=1;
/** Initialize your data structure here. */
public MyStack() { } /** Push element x onto stack. */
public void push(int x) {
if(num==1) {
queue1.add(x);
}else {
queue2.add(x);
}
} /** Removes the element on top of the stack and returns that element. */
public int pop() {
if(num==1) {
while(queue1.size()!=1) {
queue2.add(queue1.remove(0));
}
num=2;
return queue1.remove(0);
}else {
while(queue2.size()!=1) {
queue1.add(queue2.remove(0));
}
num=1;
return queue2.remove(0);
}
} /** Get the top element. */
public int top() {
if(num==1) {
return queue1.get(queue1.size()-1);
}else {
return queue2.get(queue2.size()-1);
}
} /** Returns whether the stack is empty. */
public boolean empty() {
if(num==1) {
return queue1.isEmpty();
}else {
return queue2.isEmpty();
}
}
} /**
* Your MyStack object will be instantiated and called as such:
* MyStack obj = new MyStack();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.top();
* boolean param_4 = obj.empty();
*/
最简单:
class MyStack {
private Queue<Integer> data; public MyStack() {
data = new LinkedList<>();
} public void push(int x) {
data.offer(x);
//将队列中前面已经逆序的元素放在x元素后面,使得整体逆序
for (int i = 0; i < data.size() - 1; i++) {
data.offer(data.poll());
}
} public int pop() {
return data.poll();
} public int top() {
return data.peek();
} public boolean empty() {
return data.isEmpty();
}
} /**
* Your MyStack object will be instantiated and called as such:
* MyStack obj = new MyStack();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.top();
* boolean param_4 = obj.empty();
*/
2019-03-04 23:00:41
LeetCode--255--用队列实现栈(java版)的更多相关文章
- 两个栈实现队列+两个队列实现栈----java
两个栈实现队列+两个队列实现栈----java 一.两个栈实现一个队列 思路:所有元素进stack1,然后所有出s ...
- Java实现 LeetCode 225 用队列实现栈
225. 用队列实现栈 使用队列实现栈的下列操作: push(x) – 元素 x 入栈 pop() – 移除栈顶元素 top() – 获取栈顶元素 empty() – 返回栈是否为空 注意: 你只能使 ...
- 领扣(LeetCode)用队列实现栈 个人题解
使用队列实现栈的下列操作: push(x) -- 元素 x 入栈 pop() -- 移除栈顶元素 top() -- 获取栈顶元素 empty() -- 返回栈是否为空 注意: 你只能使用队列的基本操作 ...
- [LeetCode] 226. 用队列实现栈
题目链接: https://leetcode-cn.com/problems/implement-stack-using-queues 难度:简单 通过率:59.9% 题目描述: 使用队列实现栈的下列 ...
- [Leetcode]225. 用队列实现栈 、剑指 Offer 09. 用两个栈实现队列
##225. 用队列实现栈 如题 ###题解 在push时候搞点事情:push时入队1,在把队2的元素一个个入队1,再交换队2和队1,保持队1除pushguocheng 始终为空. ###代码 cla ...
- LeetCode--155--最小栈(java版)
设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈. push(x) -- 将元素 x 推入栈中. pop() -- 删除栈顶的元素. top() -- 获取栈顶元素. ...
- 《剑指offer》面试题21 包含min函数的栈 Java版
(min函数的作用是返回栈内最小值) 首先这个栈要具有普通栈所具有的push()和pop()方法,那么内部一定包含一个Stack.至于还要能实现min函数,而且还是在O(1)时间复杂度内,我们不得不考 ...
- 剑指offer第二版面试题9:用两个队列实现栈(JAVA版)
题目:用两个队列实现栈. 分析:通过一系列的栈的压入和弹出操作来分析用队列模拟一个栈的过程,如图所示,我们先往栈内压入一个元素a.由于两个队列现在都是空,我们可以选择把a插入两个队列中的任一个.我们不 ...
- 【LeetCode题解】225_用队列实现栈(Implement-Stack-using-Queues)
目录 描述 解法一:双队列,入快出慢 思路 入栈(push) 出栈(pop) 查看栈顶元素(peek) 是否为空(empty) Java 实现 Python 实现 解法二:双队列,入慢出快 思路 入栈 ...
随机推荐
- dp专题练习
顺便开另外一篇放一些学过的各种dp dp总结:https://www.cnblogs.com/henry-1202/p/9194066.html 开坑先放15道题,后面慢慢补 目标50道题啦~~,目前 ...
- 题解——HDU 1848 Fibonacci again and again
一道组合游戏的题目 SG函数的板子题 预处理出SG函数的值然后回答询问即可 代码 #include <cstdio> #include <algorithm> #include ...
- 【转载】ASP.NET页面之间传值的方式之QueryString(个人整理)
转自: https://www.cnblogs.com/kudsu/p/7694637.html QueryString Querystring也叫查询字符串,这种页面间传递数据是利用网页地址URL. ...
- Paper Reviews and Presentations
Copied from Advanced Computer Networks, Johns Hopkins University. Paper Reviews and Presentations Ea ...
- 异步编程- async和await
使用目的 避免阻塞主线程 提高程序响应能力 C#中使用 C# 中的 Async 和 Await 关键字是异步编程的核心. 疑惑 The async and await keywords don't c ...
- ashx图片上传接收
发送数据流方法 /// <summary> /// PostBinaryData /// </summary> /// <param name="url&quo ...
- VirtualBox安装Centos6.8出现——E_INVALIDARG (0x80070057)
VirtualBox使用已有的虚拟硬盘出错: 问题描述:UUID已经存在 Cannot register the hard disk 'E:\system_iso\centos6.8.vdi' {05 ...
- Mysql 函数使用记录(一)——DATEDIFF、CONCAT
当目前为止呢,个人对Mysql的函数没有进行过统一的学习使用,都是用到了再去学习.而近日开始学习Linux了,所以为了防止这段时期结束后,将此阶段期间遇到的Mysql函数遗忘,开始在此对其做一个简单的 ...
- 【BZOJ】3143: [Hnoi2013]游走
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3143 显然如果一条边期望被走过的次数越多,我们就应该给它的编号越小. 所以问题变为如何求每 ...
- zipkin启动报错(Caused by: java.lang.ClassNotFoundException: zipkin.Component)的解决方法
使用ziplin依赖: <dependency> <groupId>org.springframework.cloud</groupId> <artifact ...