Leetcode_232_Implement Queue using Stacks
本文是在学习中的总结,欢迎转载但请注明出处: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
push to top
,peek/pop from top
,size
, andis 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)题意为用栈来实现队列。
(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(); } }
Leetcode_232_Implement Queue using Stacks的更多相关文章
- [LeetCode] Implement Queue using Stacks 用栈来实现队列
Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...
- Leetcode Implement Queue using Stacks
Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...
- Implement Queue using Stacks
Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...
- 232. Implement Queue using Stacks
Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...
- (easy)LeetCode 232.Implement Queue using Stacks
Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...
- 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】232 & 225 - Implement Queue using Stacks & Implement Stack using Queues
232 - Implement Queue using Stacks Implement the following operations of a queue using stacks. push( ...
- Java [Leetcode 232]Implement Queue using Stacks
题目描述: Implement the following operations of a queue using stacks. push(x) -- Push element x to the b ...
- LeetCode 232 Implement Queue using Stacks
Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...
随机推荐
- 剑指offer面试题3 二维数组中的查找(c)
剑指offer面试题三:
- 菜鸟学习物联网---辨析基于Andriod 5.1,Linux,Windows10开发Dragon Board 410c板
点击打开链接 诸位亲最近怎么样?刚过完年上班是不是很不情愿?自古做事者,不唯有坚韧不拔之志,亦或有超世之才.所以,诸位好好加油.今天小编想给大家系统性总结一下Dragon Board 410c板基于A ...
- Oracle使用游标为所有用户表添加主键语句
应用场合:数据表新增自增一主键能加快数据表的访问速度,而且是整形的索引速度最快.本程序适合在导入Oracle数据库时删除不存在主键的情况下运行. 代码说明:所有的表主键字段名都设置为ID,如果已存在I ...
- EBS开发技术之Patch安装
Contents Document Control........................................................................ ...
- (NO.00004)iOS实现打砖块游戏(十二):伸缩自如,我是如意金箍棒(上)!
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 通用的星星类已经完成了,下面我们来实现具体的变长和缩短道具. 变 ...
- 02-Git简单使用
Git安装(windows) https://code.google.com/p/msysgit/downloads/list 我们使用版本Git-1.7.9版本 百度网盘下载:链接:http://p ...
- pyinstaller相关错误
http://blog.csdn.net/pipisorry/article/details/50620495 目录结构 将主文件testMain.py转换成exe可执行文件 主文件调用了自定义包 ...
- cocos2dx 3.3 + QT5.3制作游戏编辑器
欢迎转载,但请注明本blog地址,谢谢_(:зゝ∠)_ http://www.cnblogs.com/marisa/p/4141862.html 主要参考: http://blog.csdn.net/ ...
- SQL备份所有数据库脚本
技巧要点:使用游标循环读取所有数据库名,然后定义存放路径,最后备份所有数据库到指定存在的本地文件夹中 脚本如下: declare @fileName varchar(255) --定义备份文件名变量d ...
- java线程池ThreadPoolExecutor 如何与 AsyncTask() 组合使用
简单说下Executors类,提供的一系列创建线程池的方法: 他们都有两个构造方法 1. --------newFixedThreadPool (创建一个定长线程池,可控制线程最大并发数,超出的线程会 ...