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 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).
解题思路:
使用两个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 ...
随机推荐
- css3选择器笔记
通用选择器ul~p{} 为ul之后的所有p标签设置属性 (ul和p为同级元素)ul+p{} 仅为ul之后的p标签设置属性 (ul和p为相邻元素)div>p 为div之后的p标签设置属性{ d ...
- PAT-乙级-1053. 住房空置率 (20)
1053. 住房空置率 (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 在不打扰居民的前提下,统计住房空 ...
- uva 514
栈的简单应用 /************************************************************************* > Author: xlc28 ...
- 如何使用yum来下载RPM包而不进行安装
如何使用yum来下载RPM包而不进行安装 2015-03-23 13:15 theo-l译 linux.cn 字号:T | T yum是基于Red Hat的系统(如CentOS.Fedora.RHEl ...
- 建立docker私有hub
docker是一个非常好用的虚拟化工具. 下面给出建立私有docker hub的方法.docker将私有hub的环境打包在registry image中 执行指令: docker run -p 500 ...
- javascript 比较
javascript中由于是弱类型,所以在比较的时候有较大的麻烦.这次专门做了总结: Comparison Operators Comparison operators are used in log ...
- 暑假集训单切赛第二场 UVA 11988 Broken Keyboard (a.k.a. Beiju Text)(字符串处理)
一开始不懂啊,什么Home键,什么End键,还以为相当于括号,[]里的东西先打印出来呢.后来果断百度了一下. 悲催啊... 题意:给定一个字符串,内部含有'['和']'光标转移指令,'['代表光标移向 ...
- javascript-权威指南读书笔记(1)
<script type="text/javascript"> //数组与object的关系 var obj = { name: "刘建伟", Ag ...
- linux入门教程(三) Linux操作系统的安装
因为笔者一直都是使用CentOS,所以这次安装系统也是基于CentOS的安装.把光盘插入光驱,设置bios光驱启动.进入光盘的欢迎界面. 其中有两个选项,可以直接按回车,也可以在当前界面下输入 lin ...
- lintcode:First Missing Positive 丢失的第一个正整数
题目 丢失的第一个正整数 给出一个无序的整数数组,找出其中没有出现的最小正整数. 样例 如果给出 [1,2,0], return 3 如果给出 [3,4,-1,1], return 2 挑战 只允许时 ...