LeetCode -- Implement Queue using Stacks
2015-09-11 21:40
567 查看
题目描述:
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.
empty() -- Return whether the queue is empty.
Notes:
You must use only standard 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).
思路:
本题考查的就是使用两个栈来实现一个队列,主要还是对栈本身的一个熟悉。
1.使用s1和s2两个栈
2.push时,直接入s2
3.pop时,先把s2的item 弹出入s1,再从s1中弹出1个,再将s1中元素逐个入s2
4.peek,类似pop,区别在于只取值但不删除。将s2的每个item弹出入s1,n = s1.pop(),然后将s1元素逐个弹入s2,返回n。
5.empty,判断s2是否为空即可。
实现代码:
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.
empty() -- Return whether the queue is empty.
Notes:
You must use only standard 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).
思路:
本题考查的就是使用两个栈来实现一个队列,主要还是对栈本身的一个熟悉。
1.使用s1和s2两个栈
2.push时,直接入s2
3.pop时,先把s2的item 弹出入s1,再从s1中弹出1个,再将s1中元素逐个入s2
4.peek,类似pop,区别在于只取值但不删除。将s2的每个item弹出入s1,n = s1.pop(),然后将s1元素逐个弹入s2,返回n。
5.empty,判断s2是否为空即可。
实现代码:
public class Queue { public Queue(){ _s1 = new Stack<int>(); _s2 = new Stack<int>(); } private Stack<int> _s1 ; private Stack<int> _s2; // Push element x to the back of queue. public void Push(int x) { _s2.Push(x); } // Removes the element from front of queue. public void Pop() { while(_s2.Count > 0){ _s1.Push(_s2.Pop()); } _s1.Pop(); while(_s1.Count > 0) { _s2.Push(_s1.Pop()); } } // Get the front element. public int Peek() { while(_s2.Count > 0){ _s1.Push(_s2.Pop()); } var n = _s1.Peek(); while(_s1.Count > 0){ _s2.Push(_s1.Pop()); } return n; } // Return whether the queue is empty. public bool Empty() { return _s2.Count == 0; } }
相关文章推荐
- WinServer 之 发布WebService后调用出现" The test form is only available for requests from the local machine. "
- knockoutjs + easyui.treegrid 可编辑的自定义绑定插件
- 导航控制器UINavigationController
- AJAX POST请求中参数以form data和request payload形式在php中的获取方式
- LeetCode(63)Unique Paths II
- LeetCode(63)Unique Paths II
- UI界面编写(仿QQ聊天界面)
- MySQL JDBC的queryTimeout坑
- LeetCode(62)Unique Paths
- LeetCode(62)Unique Paths
- iOS8下UILabel如何自适应高度(自动换行)
- IOS视图之UI基础
- TrueCrypt加密之后的取证方法(广东公安科技)
- UISlider
- UISegmetControl
- Machine Intelligence(Nature Insight 2015.5.28/Vol 521/Issue No 7553)
- IOS_UI_数据库
- UI:这段时间的小总结
- iOS开发之 UIAlertView 沙盒 归档
- iOS开发之 UITextField