Leetcode_232_Implement Queue using Stacks
2015-09-12 11:01
375 查看
本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/48392363
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
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)题意为用栈来实现队列。
(2)要用栈来实现队列,首先需要了解栈和队列的性质。栈:先进后出,只能在栈顶增加和删除元素;队列:先进先出,只能在队尾增加元素,从队头删除元素。这样,用栈实现队列,就需要对两个栈进行操作,这里需要指定其中一个栈为存储元素的栈,假定为stack2,另一个为stack1。当有元素加入时,首先判断stack2是否为空(可以认为stack2是目标队列存放元素的实体),如果不为空,则需要将stack2中的元素全部放入(辅助栈)stack1中,这样stack1中存储的第一个元素为队尾元素;然后,将待加入队列的元素加入到stack1中,这样相当于实现了将入队的元素放入队尾;最后,将stack1中的元素全部放入stack2中,这样stack2的栈顶元素就变为队列第一个元素,对队列的pop和peek的操作就可以直接通过对stack2进行操作即可。
(3)详情见下方代码。希望本文对你有所帮助。
算法代码实现如下:
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 emptyoperations 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)题意为用栈来实现队列。
(2)要用栈来实现队列,首先需要了解栈和队列的性质。栈:先进后出,只能在栈顶增加和删除元素;队列:先进先出,只能在队尾增加元素,从队头删除元素。这样,用栈实现队列,就需要对两个栈进行操作,这里需要指定其中一个栈为存储元素的栈,假定为stack2,另一个为stack1。当有元素加入时,首先判断stack2是否为空(可以认为stack2是目标队列存放元素的实体),如果不为空,则需要将stack2中的元素全部放入(辅助栈)stack1中,这样stack1中存储的第一个元素为队尾元素;然后,将待加入队列的元素加入到stack1中,这样相当于实现了将入队的元素放入队尾;最后,将stack1中的元素全部放入stack2中,这样stack2的栈顶元素就变为队列第一个元素,对队列的pop和peek的操作就可以直接通过对stack2进行操作即可。
(3)详情见下方代码。希望本文对你有所帮助。
算法代码实现如下:
package leetcode; import java.util.Stack; /** * @author liqqc * */ public class Implement_Queue_using_Stacks { public Stack<Integer> _stack1 = new Stack<Integer>(); public Stack<Integer> _stack2 = new Stack<Integer>(); public void push(int x) { while (!_stack2.isEmpty()) { _stack1.push(_stack2.pop()); } _stack1.push(x); while (!_stack1.isEmpty()) { _stack2.push(_stack1.pop()); } } // Removes the element from in front of queue. public void pop() { _stack2.pop(); } // Get the front element. public int peek() { return _stack2.peek(); } // Return whether the queue is empty. public boolean empty() { return _stack2.isEmpty(); } }
相关文章推荐
- Lettcode_232_Implement Queue using Stacks
- 用block改写UIButton点击事件和UIAlerView的按钮点击代理方法
- [iOS UI进阶 - 5.0] 手势解锁Demo
- 【Java】_图形用户界面(GUI)
- 注意uiview的两个重绘函数
- UI中一系列 个人错误总结
- [Leetcode]Closest Binary Search Tree Value
- [Leetcode]Closest Binary Search Tree Value II
- UISearchBar 的相关属性和delegate
- UIScrollView/UIPageControl/
- 改变UITextField placeHolder颜色、字体 、输入光标位置等
- easy ui 表单提交添加遮罩,避免数据重复提交
- Builder模式和Prototype模式-都有面向接口基类
- iOS开发——改变UITextField的样式:占位符颜色、字符起始位置等
- UITableView是不会响应touchesBegan:方法的
- 将UIAlertView的按钮点击代理方式改为Block形式
- Scala 深入浅出实战经典 第98讲:使用SBT开发时动手解决rt.jar中CharSequence is broken等问题
- Leetcode: Count Univalue Subtrees
- 常见安卓主题显示
- Flash Stage3D 在2D UI 界面上显示3D模型问题完美解决