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 ...
随机推荐
- [转载]C# 多线程、控制线程数提高循环输出效率
C#多线程及控制线程数量,对for循环输出效率. 虽然输出不规律,但是效率明显提高. 思路: 如果要删除1000条数据,只使用for循环,则一个接着一个输出.所以,把1000条数据分成seed段,每段 ...
- uva 10651
DP 主要是位运算的运用 因为只有12位 用一个数字代表一种装态 记忆化搜索 节约时间 /***************************************************** ...
- DelayedOperationPurgatory之DelayedOperation pool
purgatory就是炼狱的意思. 当一个DelayedOperation需要被delay时,它就被放到DelayedOperationPurgatory,相当于进行一个等待池.上一篇blog提到过, ...
- A const field of a reference type other than string can only be initialized with null Error [duplicate]
I'm trying to create a 2D array to store some values that don't change like this. const int[,] hiveI ...
- Asp.net MVC 自定义路由在IIS7以上,提示Page Not Found 解决方法
受限确保自定义路由在开发服务器上Ok! 然后在web.config的<webserver>节点下增加如下配置就好了. 1: <system.webServer> 2: &l ...
- Searching a 2D Sorted Matrix Part I
Write an efficient algorithm that searches for a value in an n x m table (two-dimensional array). Th ...
- UVA 10523 Very Easy!!!(大数据加法、乘法)
题意:给出N,A,计算i*A^i(i=1~N)的和.1<=N<=30,0<=A<=15. 就是大数据运算,别的没什么,注意细节之处即可. 这题还要注意两个地方: 1.考虑A=0 ...
- ubuntu下hadoop2.6在eclipse上的配置
1.复制hadoop-eclipse-plugin-2.6.0.jar插件到eclipse的plugins目录下(hadoop2.6的插件,在hadoop自带上没有,要上网下载,多试几个 如果是从ub ...
- IOS中实现图片点击全屏预览
//// ViewController.m// XWZoomImageView//// Created by xiao on 15/11/13.// Copyright © 2015年 xiao. A ...
- Servlet小示例:jsp页面提交信息Servlet接收并打印输出
该示例采用doPost方法提交表单,该示例一共包含两个文件. 一个是用来提交用户信息的表单userForm2.jsp,另一个是用来接收参数的Servlet. userForm2.jsp <%@ ...