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

  • 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. linux内核中的fuse是什么?

    答: 一个用户态文件系统框架,属于内核的一种特性. 1.组成部分 fuse.ko(内核模块) + libfuse.*(用户空间库) + fusemount(挂载工具) 2.参考资料 fuse.txt

  2. Manjaro 系统添加国内源和安装搜狗输入法

    添加中科大源 #打开配置文件 kate /etc/pacman.conf 在文件末尾添加 [archlinuxcn] SigLevel = Optional TrustedOnly Server = ...

  3. Smartmontools硬盘检测工具

    安装: 下载 命令使用: 查看驱动器信息: $ smartctl -i D: smartctl 6.6 2017-11-05 r4594 [x86_64-w64-mingw32-w10-b17134] ...

  4. CentOS7.2 问题收集 查看文件大小 查看端口

    1.在vmware中使用nat模式安装centos7.2,没有ifconfig命令? yum upgrade yum install net-tools 2.查看当前目录所有文件大小 [root@lo ...

  5. 【做题】51NOD1518 稳定多米诺覆盖——容斥&dp

    题意:求有多少种方案,用多米诺骨牌覆盖一个\(n\times m\)的棋盘,满足任意一对相邻行和列都至少有一个骨牌横跨.对\(10^9+7\)取模. \(n,m \leq 16\) 首先,这个问题的约 ...

  6. JQuery的ajax方法获取返回值得到了值还包含了不要的html源码 (Ajax相关知识)

    因为后台使用了response.Write():这个方法,当输出完了以后,没有结束掉会继续输出,所以需要用到response.End():这样问题就解决了 jquery的ajax 除了通过url传值, ...

  7. MongoDB集群配置笔记一

    MongoDB 的部署方案有单机部署.复本集(主备)部署.分片部署.复本集与分片混合部署.混合的部署方式如图: 分片集群的构造 (1)mongos :数据路由,和客户端打交道的模块.mongos本身没 ...

  8. spring与mybatis四种整合方法

    转载: 1.采用数据映射器(MapperFactoryBean)的方式,不用写mybatis映射文件,采用注解方式提供相应的sql语句和输入参数.   (1)Spring配置文件: <!-- 引 ...

  9. ESP8266 使用

    ESP8266 微机使用串口和ESP8266建立通信,ESP8266把消息通过wifi发送出去 助手软件 网络调试助手 串口调试助手 AT指令 指令 作用 AT 测试是否进入AT模式 AT+RST 重 ...

  10. GYM 101064 2016 USP Try-outs G. The Declaration of Independence 主席树

    G. The Declaration of Independence time limit per test 1 second memory limit per test 256 megabytes ...