Java [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 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).
解题思路:
使用两个stack,一个用于插入数据,另一个用于输出数据。
代码如下:
class MyQueue {
Stack<Integer> input = new Stack<Integer>();
Stack<Integer> output = new Stack<Integer>();
// Push element x to the back of queue.
public void push(int x) {
input.push(x);
}
// Removes the element from in front of queue.
public void pop() {
peek();
output.pop();
}
// Get the front element.
public int peek() {
if(output.empty()){
while(!input.empty()){
output.push(input.pop());
}
}
return output.peek();
}
// Return whether the queue is empty.
public boolean empty() {
return input.empty() && output.empty();
}
}
Java [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 ...
- 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 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 ...
随机推荐
- ubuntu中磁盘挂载与卸载
问题描述: ubuntu中磁盘的挂载和卸载 问题解决: (1)ubuntu中磁盘挂载 注: 如上所示,使用命令df查看磁盘使用情况 ...
- 剑指offer--面试题8
题目:求旋转数组中的最小数字 以下为自己所写代码: #include "stdafx.h" #include <iostream> #include <excep ...
- HTTP 错误 403.14 - Forbidden
在打开一个网站时,显示HTTP 错误 403.14 - Forbidden 是一件很不幸的事情.我这几天打开某网站就出现了这个问题.Web 服务器被配置为不列出此目录的内容,错误代码0x0000000 ...
- [设计模式] 5 单例模式 singleton
转处 http://blog.csdn.net/wuzhekai1985 软件领域中的设计模式为开发人员提供了一种使用专家设计经验的有效途径.设计模式中运用了面向对象编程语言的重要特性:封装.继承.多 ...
- 01-03-02-1【Nhibernate (版本3.3.1.4000) 出入江湖】CRUP操作--cascade 级联相关
要点: 1. <!--双向关联时要用: inverse:由子表来维护关系,cascade:级联的关系 如果没有这个设置, 插入Customer成功(即使现在Order插入Order抛异常,这时产 ...
- jsp的常用指令有哪些(编译指令/动作指令整理)
jsp的常用指令有哪些(编译指令/动作指令整理) JSP动作指令 JSP - JSP中的脚本.指令.动作和注释
- 深入浅出ES6(四):模板字符串
作者 Jason Orendorff github主页 https://github.com/jorendorff 反撇号(`)基础知识 ES6引入了一种新型的字符串字面量语法,我们称之为模板字符 ...
- **tomcat简介之web.xml详解
一.Tomcat背景 自从JSP发布之后,推出了各式各样的JSP引擎.Apache Group在完成GNUJSP1.0的开发以后,开始考虑在SUN的JSWDK基础上开发一个可以直接提供Web服务的JS ...
- form表单中的enctype属性什么意思?
enctype就是encodetype翻译成中文就是编码类型的意思!multipart/form-data是指表单数据有多部分构成:既有文本数据,又有文件等二进制数据的意思.另外需要注意的是:默认情况 ...
- 【PSR规范专题(1)】PSR-0+namespace+spl_autoload_register实现框架模型
了解命名空间 namespace是PHP5.3版本加入的新特性,用来解决在编写类库或应用程序时创建可重用的代码如类或函数时碰到的两类问题: 用户编写的代码与PHP内部的类/函数/常量或第三方类/函数/ ...