LeetCode(36)- Implement Stack using Queues
题目:
Implement the following operations of a stack using queues.
push(x) -- Push element x onto stack.
pop() -- Removes the element on top of the stack.
top() -- Get the top element.
empty() -- Return whether the stack is empty.
Notes:
You must use only standard operations of a queue -- which means only push to back, peek/pop from front, size, and is empty operations are valid.
Depending on your language, queue may not be supported natively. You may simulate a queue by using a list or deque (double-ended queue), as long as you use only standard operations of a queue.
You may assume that all operations are valid (for example, no pop or top operations will be called on an empty stack).
Update (2015-06-11):
The class name of the Java function had been updated to MyStack instead of Stack.
思路:
- 题意是用队列数据结构来实现栈,要求只能使用Queue的基本函数,包括poll,isEmprty等
- 用两个队列pre和next来实现,pop()操作是把一个非空队列复制到另一个空队列,最后一个不复制,每次判断哪个非空
- push(int x)的实现:先判断哪个队列非空,然后添加x,isEmpty()是两个队列都为空的时候
-
代码:
import java.util.LinkedList;
import java.util.Queue;
class MyStack {
Queue<Integer> pre = new LinkedList<Integer>();
Queue<Integer> next = new LinkedList<Integer>();
// Push element x onto stack.
public void push(int x) {
if(next != null){
next.add(x);
}else{
pre.add(x);
}
}
// Removes the element on top of the stack.
public void pop() {
if(!next.isEmpty()){
while(!next.isEmpty()){
int a = next.poll();
if(!next.isEmpty()){
pre.add(a);
}
}
}else{
while(!pre.isEmpty()){
int b = pre.poll();
if(!pre.isEmpty()){
next.add(b);
}
}
}
}
// Get the top element.
public int top() {
if(!next.isEmpty()){
while(!next.isEmpty()){
int c = next.poll();
pre.add(c);
if(next.isEmpty()){
return c;
}
}
}else{
while(!pre.isEmpty()){
int d = pre.poll();
next.add(d);
if(pre.isEmpty()){
return d;
}
}
}
return 0;
}
public boolean empty() {
if(next.isEmpty() && pre.isEmpty()){
return true;
}else{
return false;
}
}
}
LeetCode(36)- Implement Stack using Queues的更多相关文章
- 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 225 Implement Stack using Queues(用队列来实现栈)(*)
翻译 用队列来实现栈的例如以下操作. push(x) -- 将元素x加入进栈 pop() -- 从栈顶移除元素 top() -- 返回栈顶元素 empty() -- 返回栈是否为空 注意: 你必须使用 ...
- [LeetCode] 225. 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 ...
- (easy)LeetCode 225.Implement Stack using Queues
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- Leetcode 225 Implement Stack using Queues
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- Java [Leetcode 225]Implement Stack using Queues
题目描述: Implement the following operations of a stack using queues. push(x) -- Push element x onto sta ...
- Leetcode 225 Implement Stack using Queues STL
用两个队列去实现栈,这里我使用了队列数组q[2],在所有的过程中保证一个队列是空的 push时插入到空的队列中,然后将队列中的元素移到另一个队列中 pop时从不空的队列中pop() peek时从不空的 ...
- LeetCode 225 Implement Stack using Queues 用队列实现栈
1.两个队列实现,始终保持一个队列为空即可 class MyStack { public: /** Initialize your data structure here. */ MyStack() ...
随机推荐
- JDBC数据库连接简介(一)
jdbc的由来 odbc(open database connection) 最初各个数据库比如mysql和oracle等,虽然都支持sql,但是他们的连接方式是不一样的,需要按照相应的api来编写不 ...
- Retrofit 2.0 超能实践(一),okHttp完美支持Https传输
http: //blog.csdn.net/sk719887916/article/details/51597816 Tamic首发 前阵子看到圈子里Retrofit 2.0,RxJava(Andro ...
- Android初级教程:屏幕分辨率
在app编码中经常需要获取手机的屏幕分辨率(宽*高),原来我直接上网拷贝代码,但在使用过程中却发现诸多不便. 不便一:下面代码中的getWidth和getHeight在adt上提示deprecated ...
- 登录ssh时Host key verification failed错误
工作中总是测试不同的路由设备, 路由器的ip都是 192.168.111.1 ,ssh登录的时候总是出现这个错误. macos上,错误如下 spawn ssh -p 22 root@192.168.1 ...
- Maven原型骨架及常见问题
关于Maven原型工程的制作就不在这里详细介绍了,具体细节请参考之前的文章:定制Maven原型生成项目 下面分享制作和使用Maven原型工程时碰到的常见问题,以及原型的上传和使用方法. 1.模块路径问 ...
- (NO.00005)iOS实现炸弹人游戏(六):游戏数据的初始化(三)
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 现在我们来看看实际初始化地图的randomCreateMap方法 ...
- 【一天一道LeetCode】#122. Best Time to Buy and Sell Stock II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Say you ...
- visual studio 2015使用python tools远程调试maya 2016
步骤: 1. 去https://apps.exchange.autodesk.com/MAYA/en/Home/Index搜索Developer Kit并下载,maya 2016可以直接点击这里下载. ...
- UNIX环境高级编程——创建与打开IPC通道
创建或打开一个IPC对象的三个getXXX函数的第一个参数key是类型为key_t的IPC键,返回值identifier是一个整数标识符.该标识符不同于ftok函数的id参数.对于key值,应用程序有 ...
- IE浏览器打印的页眉页脚设置解决方法
首先说明问题: 默认情况下,通过IE的打印对话框,打印出来的内容都有页眉和页脚的. 查看ie的页面设置发现如右图中,页眉页脚 下面先说明&w&bPage&p of &P ...