使用队列实现栈的下列操作:

  • push(x) -- 元素 x 入栈
  • pop() -- 移除栈顶元素
  • top() -- 获取栈顶元素
  • empty() -- 返回栈是否为空

注意:

  • 你只能使用队列的基本操作-- 也就是 push to backpeek/pop from frontsize, 和 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版)的更多相关文章

  1. 两个栈实现队列+两个队列实现栈----java

                                               两个栈实现队列+两个队列实现栈----java 一.两个栈实现一个队列 思路:所有元素进stack1,然后所有出s ...

  2. Java实现 LeetCode 225 用队列实现栈

    225. 用队列实现栈 使用队列实现栈的下列操作: push(x) – 元素 x 入栈 pop() – 移除栈顶元素 top() – 获取栈顶元素 empty() – 返回栈是否为空 注意: 你只能使 ...

  3. 领扣(LeetCode)用队列实现栈 个人题解

    使用队列实现栈的下列操作: push(x) -- 元素 x 入栈 pop() -- 移除栈顶元素 top() -- 获取栈顶元素 empty() -- 返回栈是否为空 注意: 你只能使用队列的基本操作 ...

  4. [LeetCode] 226. 用队列实现栈

    题目链接: https://leetcode-cn.com/problems/implement-stack-using-queues 难度:简单 通过率:59.9% 题目描述: 使用队列实现栈的下列 ...

  5. [Leetcode]225. 用队列实现栈 、剑指 Offer 09. 用两个栈实现队列

    ##225. 用队列实现栈 如题 ###题解 在push时候搞点事情:push时入队1,在把队2的元素一个个入队1,再交换队2和队1,保持队1除pushguocheng 始终为空. ###代码 cla ...

  6. LeetCode--155--最小栈(java版)

    设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈. push(x) -- 将元素 x 推入栈中. pop() -- 删除栈顶的元素. top() -- 获取栈顶元素. ...

  7. 《剑指offer》面试题21 包含min函数的栈 Java版

    (min函数的作用是返回栈内最小值) 首先这个栈要具有普通栈所具有的push()和pop()方法,那么内部一定包含一个Stack.至于还要能实现min函数,而且还是在O(1)时间复杂度内,我们不得不考 ...

  8. 剑指offer第二版面试题9:用两个队列实现栈(JAVA版)

    题目:用两个队列实现栈. 分析:通过一系列的栈的压入和弹出操作来分析用队列模拟一个栈的过程,如图所示,我们先往栈内压入一个元素a.由于两个队列现在都是空,我们可以选择把a插入两个队列中的任一个.我们不 ...

  9. 【LeetCode题解】225_用队列实现栈(Implement-Stack-using-Queues)

    目录 描述 解法一:双队列,入快出慢 思路 入栈(push) 出栈(pop) 查看栈顶元素(peek) 是否为空(empty) Java 实现 Python 实现 解法二:双队列,入慢出快 思路 入栈 ...

随机推荐

  1. python --- 10 *args **kwargs 命名空间 作用域 函数的嵌套

    函数进阶 1.   "*"  和  "**" ① 在形参位置时   都是聚合的作用 *args    位置参数→元组 **kwargs   关键字参数→字典 混 ...

  2. easyui中datagrid常见功能

    1.数据加载,需要拼接成标准json格式{}.如果是jsonarray格式[{},{}],无法识别. 2.后端将list拼接成datagrid能识别的json格式,需要首先new JSONObject ...

  3. Installing Jenkins as a Windows service

    Install Jenkins as a Windows service NOTE: if you installed Jenkins using the windows installer, you ...

  4. topcoder srm 686 div1

    problem1 link 左括号和右括号较少的一种不会大于20.假设左括号少.设$f[i][mask][k]$表示处理了前$i$个字符,其中留下的字符以$k$开头($k=0$表示'(',$k=1$表 ...

  5. upc组队赛1 流连人间的苏苏

    流连人间的苏苏 题目描述 苏苏在做红尘仙的任务时,发现坐落于风景秀丽.四季如春的昆明市的云南中医学院. 没过多久,苏苏就喜欢上了这个学校.以致于苏苏忘了回涂山的时间,现在她只剩下d天的时间待在云南中医 ...

  6. 如何清除浮动塌陷? float:left 塌陷

    参考文章: http://www.jb51.net/css/502268.html 原文链接:http://www.jianshu.com/p/a0500b54da13 只要给li的宽度, 规定得小一 ...

  7. [NOI1995]石子合并 四边形不等式优化

    链接 https://www.luogu.org/problemnew/show/P1880 思路 总之就是很牛逼的四边形不等式优化 复杂度\(O(n^2)\) 代码 #include <ios ...

  8. office完全卸载

    第一步:先暂停office服务,再通过 控制面板--卸载程序  --卸载office应用 第二步:通过office_move(自己命名的工具)软件卸载  工具分享:https://pan.baidu. ...

  9. springmvc异步上传图片并回调页面函数插入图片url代码示例

    <tr> <td class="search_td">属性值图片值:</td> <td> <input type=" ...

  10. dRMT: Disaggregated Programmable Switching, SIGCOMM17

    Reference: dRMT, SIGCOMM 2017 今年的SIGCOMM17会议上,Cisco System和MIT的团队针对RMT模型现有的问题,合作发表了这篇"dRMT: Dis ...