LeetCode--Implement Queue using Stacks
2015-11-30 17:33
411 查看
Problem:
Implement the following operations of a queue using stacks.
- push(x) – Push element x to the back of queue.
- pop() – Removes the element from in front of queue.
- peek() – Get the front element.
Notes:
- You must use onlystandard operations of a stack – which means only push to top,peek/pop from top, size, and is empty operations are valid.
- Depending on your language, stack may not be supported natively. You may simulate a stack by using a list or deque (double-ended queue), as long as you use only standard operations of a stack.
- You may assume that all operations are valid (for example, no pop or peek operations will be called on an empty queue).
Analysis:
题意:用栈实现队列
用两个堆栈实现,一个放push的数据,另一个放pop的数据,但是在pop在前需要将push栈中的数据push到pop栈中,这样就可以使pop先进先出了。
注意:
堆栈的输入元素是对象,所以需要定义Integer类型
pop时应该先pop堆栈(pop的),如果为空就要将push堆栈的数据push到pop堆栈中,然后再pop出去。
peek需要返回int类型,所以注意输出时return,不要忘了。
判断是否为空主要是判断两个栈是否同时为空。
Anwser:
Implement the following operations of a queue using stacks.
- push(x) – Push element x to the back of queue.
- pop() – Removes the element from in front of queue.
- peek() – Get the front element.
Notes:
- You must use onlystandard operations of a stack – which means only push to top,peek/pop from top, size, and is empty operations are valid.
- Depending on your language, stack may not be supported natively. You may simulate a stack by using a list or deque (double-ended queue), as long as you use only standard operations of a stack.
- You may assume that all operations are valid (for example, no pop or peek operations will be called on an empty queue).
Analysis:
题意:用栈实现队列
用两个堆栈实现,一个放push的数据,另一个放pop的数据,但是在pop在前需要将push栈中的数据push到pop栈中,这样就可以使pop先进先出了。
注意:
堆栈的输入元素是对象,所以需要定义Integer类型
pop时应该先pop堆栈(pop的),如果为空就要将push堆栈的数据push到pop堆栈中,然后再pop出去。
peek需要返回int类型,所以注意输出时return,不要忘了。
判断是否为空主要是判断两个栈是否同时为空。
Anwser:
class MyQueue { // Push element x to the back of queue. Stack<Integer> in = new Stack<>(); Stack<Integer> out = new Stack<>(); public void push(int x) { in.push(x); } // Removes the element from in front of queue. public void pop() { if (!out.empty()) out.pop(); else { while(!in.empty()) out.push(in.pop()); out.pop(); } } // Get the front element. public int peek() { if (!out.empty()) return out.peek(); else { while(!in.empty()) out.push(in.pop()); return out.peek(); } } // Return whether the queue is empty. public boolean empty() { if(in.empty()&& out.empty()) return true; else return false; } }
相关文章推荐
- iOS UIKit中支持拷贝-粘贴操作 详解
- 还等什么,陪孩子玩转这些IT项目
- 两个UITableView 嵌套滑动问题解决
- 多控制器UINavigationController
- UIButton之自定义button
- 研究 UIActivityViewController
- [原]Error Domain=com.alamofire.error.serialization.response Code=-1016 "Request failed: unacceptable con
- MySQL数据库出现The server quit without updating PID file.
- UI- font 字体设置
- UI- font 字体设置
- iOS自定义UIAlertView的小tips
- Android UI优化——include、merge 、ViewStub
- CSS中滚动效果<MARQUEE>的用法
- UIDatePicker时区问题的解决
- easyui--layout布局
- iOS开发拓展篇—UIDynamic(重力行为+碰撞检测)
- 分析mysql的QueryCache在相同语义SQL是否可以使用查询缓存
- iOS开发拓展篇—UIDynamic(捕捉行为)
- iOS UIActivityIndicatorView 的使用(旋转菊花)
- iOS UIActivityIndicatorView 的使用(旋转菊花)