leetcode Implement Queue using Stacks
来源:互联网 发布:mysql安装包百度云 编辑:程序博客网 时间:2024/05/01 07:55
继续刷题。编译CM12.1刷机怎么着都有问题,烦人的把之前下载的源码直接删掉,重新下载,在下载源码期间继续刷题,希望这个easy不要把我虐成狗。
其实主要问题是自己米有java基础知识,幸亏会点C,可以根据C又得东西查java,但这样的一个弊端时,java把C很多东西封装成了自己的类,很多东西我并不知道有那个东西的存在。。 不废话了,看题;
一. 题目
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).
二. java知识点学习
堆栈类 stack:Stack继承Vector类 stack.push / .pop /.peek /.search / .empty http://www.jb51.net/article/44645.htm
Stack stack = new Stack();
peek() 方法用于查找在此堆栈顶部的对象,无需从堆栈中取出。
public int search(Object o) 方法用于返回在栈中从1开始的位置的一个对象 的位置
队列 Queue: java.util.Queue接口,用以支持队列的常见操作 http://www.cnblogs.com/linjiqin/archive/2013/05/30/3107656.html
值得注意的是LinkedList类实现了Queue接口,因此我们可以把LinkedList当成Queue来用。
Queue<String> queue = new LinkedList<String>();
Queue使用时要尽量避免Collection的add()和remove()方法,而是要使用offer()来加入元素,使用poll()来获取并移出元素。它们的优点是通过返回值可以判断
成功与否,add()和remove()方法在失败的时候会抛出异常。 如果要使用前端而不移出该元素,使用element()或者peek()方法。
三 . 代码实现
思路:利用两个栈来实现一个队列。 进栈的时候不做任何处理, 出栈的时候先将该栈里面的数据放到另一个栈里面,从另一栈进行出栈。
Runtime: 208 ms
class MyQueue { Stack<Integer>stack1 = new Stack<>(); Stack<Integer>stack2 = new Stack<>(); // Push element x to the back of queue. public void push(int x) { //相当于Queue的offer()操作:stack里面的push操作是先进后出,即放到栈顶。 而Queue的push操作先进先出。即放到队列尾部。 stack1.push(x); } // Removes the element from in front of queue. public void pop() { //相当于Queue的poll()操作 if(!stack2.isEmpty()) stack2.pop(); //之前已经进行过pop操作,还要继续进行的。 else{ while(!stack1.isEmpty()){ stack2.push(stack1.pop()); } stack2.pop(); //注意这里,等第一个stack全部放到第二个stack后才进行pop操作 } } // Get the front element. public int peek() { //相当于Queue的element()或者peek()操作 if(!stack2.isEmpty()) return stack2.peek(); //之前已经进行过pop操作,还要继续进行的。 else{ while(!stack1.isEmpty()){ stack2.push(stack1.pop()); } return stack2.peek(); } } // Return whether the queue is empty. public boolean empty() { //相当于Queue的 return stack1.isEmpty() && stack2.isEmpty(); }}
- leetcode :Implement Queue using Stacks
- [LeetCode] Implement Queue using Stacks
- LeetCode Implement Queue using Stacks
- LeetCode || Implement Queue using Stacks
- leetcode Implement Queue using Stacks
- Leetcode Implement Queue using Stacks
- [LeetCode]Implement Queue using Stacks
- 【Leetcode】Implement Queue using Stacks
- LeetCode Implement Queue using Stacks
- Leetcode: Implement Queue using Stacks
- [leetcode] Implement Queue using Stacks
- 【leetcode】Implement Queue using Stacks
- [Leetcode]Implement Queue using Stacks
- [leetcode] Implement Queue using Stacks
- LeetCode-Implement Queue using Stacks
- LeetCode Implement Queue using Stacks
- Leetcode: Implement Queue using Stacks
- LeetCode:Implement Queue using Stacks
- IOS支付宝集成总结
- 20150714学校测试
- java中的return关键字
- WinServer2008 R2下部署SQL 2008 群集(Part1)
- OpenCV与Compressive Tracking实现人脸的实时检测与跟踪
- leetcode Implement Queue using Stacks
- gdb调试
- C++ STL学习之stack。http://blog.csdn.net/wallwind/article/details/6858634
- 数据库SQL操作指令
- Bootstrap select多选下拉框赋值
- Java基础——集合框架(一)
- stl之hash_map
- Android应用Activity、Dialog、PopWindow、Toast窗口添加机制及源码分析 《三》-Dialog
- 关于时间,日期,星期,月份的算法(Java中Calendar的使用方法)