LeetCode 232 Implement Queue using Stacks
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).
- You must use only standard operations of a stack -- which means only
思路:
利用两个栈,每次入队的操作时,将一个栈中的元素全部弹出,后在有元素的栈中压入入队元素,后将该栈的全部元素弹出,压回第一个栈中即可。
解法:
import java.util.Stack; public class MyQueue { private Stack<Integer> inputStack; private Stack<Integer> outputStack; public MyQueue() { inputStack = new Stack<>(); outputStack = new Stack<>(); } public void push(int x) { while(!outputStack.isEmpty()) inputStack.push(outputStack.pop()); inputStack.push(x); while(!inputStack.isEmpty()) outputStack.push(inputStack.pop()); } public void pop() { outputStack.pop(); } public int peek() { return outputStack.peek(); } public boolean empty() { return outputStack.isEmpty(); } }
LeetCode 232 Implement Queue using Stacks的更多相关文章
- Leetcode 232 Implement Queue using Stacks 和 231 Power of Two
1. 232 Implement Queue using Stacks 1.1 问题描写叙述 使用栈模拟实现队列.模拟实现例如以下操作: push(x). 将元素x放入队尾. pop(). 移除队首元 ...
- [LeetCode] 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 ...
- Java [Leetcode 232]Implement Queue using Stacks
题目描述: Implement the following operations of a queue using stacks. push(x) -- Push element x to the b ...
- Java for LeetCode 232 Implement Queue using Stacks
Stack<Integer> stack=new Stack<Integer>(); public void push(int x) { stack.push(x); } // ...
- Leetcode 232 Implement Queue using Stacks STL
本题用两个栈实现队列,用栈的基本操作去实现队列的所有基本操作push(),pop(),peek()以及empty() sa作为输入栈,sb作为输出栈,将sa输入元素的反转过来放到sb中 push与sa ...
- LeetCode 232 Implement Queue using Stacks 两个栈实现队列
class MyQueue { public: /** Initialize your data structure here. */ MyQueue() { } /** Push element x ...
- leetcode 155. Min Stack 、232. Implement Queue using Stacks 、225. Implement Stack using Queues
155. Min Stack class MinStack { public: /** initialize your data structure here. */ MinStack() { } v ...
- 232. Implement Queue using Stacks,225. Implement Stack using Queues
232. Implement Queue using Stacks Total Accepted: 27024 Total Submissions: 79793 Difficulty: Easy Im ...
随机推荐
- 5、处理模型数据ModelAndView、Map、Model以及@SessionAttributes注解
Spring MVC提供了以下几种途径输出模型数据 —— ModelAndView: 处理方法返回值类型为ModelAndView时,方法体即可通过该对象添加模型数据.数据会添加到request域中. ...
- opencv求取RGB分量
需要注意的是下面r,b,g的类型和顺序 须用IPL_DEPTH_8U类型创建图像且[0][1][2]分量分别是b,g,r. 另外多谢郑乾师兄帮我发现了IPL_DEPTH_8U问题 uchar r,b, ...
- [Codeforces137A]Postcards and photos(模拟)
题目链接:http://codeforces.com/contest/137/problem/A 题意:一个人搬东西,每次只能搬相同的东西,最多只能搬相同的东西不超过5个.问最少搬多少次. 模拟就行了 ...
- C++STL之map的基本操作
STL中基本的关联式容器有map和set,它们都是以红黑树作为其底层的结构,具有非常高的查找.删除效率,内容会按照键值自动排序. 使用map的注意事项: 1.关联式容器的键值是不允许修改的,所以永远不 ...
- EJB3Persistence开发手册-原生SQL查询(NativeSQL)
EJB3 QL对原生SQL做了非常好的支持.采用原生SQL做查询结果不但可以是象SQL中的返回列值,也可以是Entity类,甚至可以是两者的混合. EJB3 EntityManager接口定义了多个原 ...
- Hibernate+JPA (EntityMange讲解)
近年来ORM(Object-Relational Mapping)对象关系映射,即实体对象和数据库表的映射)技术市场人声音鼎沸,异常热闹, Sun在充分吸收现有的优秀ORM框架设计思想的基础上,制定了 ...
- 【Todo】JS跨域访问问题的解决
做双十一,需要在主会场页面,嵌入我们产品的JS豆腐块.而这个豆腐块需要调用我们后端的数据接口,涉及跨域访问. 参考 http://www.cnblogs.com/2050/p/3191744.html ...
- JS闭包的两个使用方向
直接上代码,备用,详见注释 <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="serve ...
- CodeForces Round #279 (Div.2)
A: 题意: 有三个项目和n个学生,每个学生都擅长其中一个项目,现在要组成三个人的队伍,其中每个人恰好擅长其中一门,问能组成多少支队伍. 分析: 最多能组成的队伍的个数就是擅长项目里的最少学生. #i ...
- listagg 函数
listagg 函数--oracle 11g release 2 转载:http://xpchild.blog.163.com/blog/static/10180985920108485721969/ ...