(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 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).
代码如下:
class MyQueue {
// Push element x to the back of queue.
Stack<Integer> stack1=new Stack<Integer>();
Stack<Integer> stack2=new Stack<Integer>();
public void push(int x) {
stack1.push(x);
}
// Removes the element from in front of queue.
public void pop() {
while(stack1.size()>1) stack2.push(stack1.pop());
stack1.pop();
while(stack2.size()>0) stack1.push(stack2.pop());
}
// Get the front element.
public int peek() {
while(stack1.size()>1) stack2.push(stack1.pop());
int x= stack1.peek();
while(stack2.size()>0) stack1.push(stack2.pop());
return x;
}
// Return whether the queue is empty.
public boolean empty() {
return stack1.empty();
}
}
运行结果:

(easy)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 ...
- 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 ...
- 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 ...
随机推荐
- Sql2008 全文索引应用(错误7625)
在SQL Server 中提供了一种名为全文索引的技术,可以大大提高从长字符串里搜索数 据的速度,不用在用LIKE这样低效率的模糊查询了. 下面简明的介绍如何使用Sql2008 全文索引 一.检查服务 ...
- 【性能诊断】七、并发场景的性能分析(windbg案例,线程阻塞)
简单整理一个测试Demo,抓取dump并验证,步骤如下: Symbol File Path:SRV*C:\Symbols*http://msdl.microsoft.com/download/symb ...
- java读取properties文件的内容
获得properties文件中某个key对应着的value // 路径名称 + properties的名称,不要“properties” private static final String BUN ...
- VBA実績表
VBA 插入一行保留样式 VBA 打开一个string指定的文件 VBA 按照文件类型名称打开一个文件 VBA excel中表示列的字母换成数字
- 【转】ODBC、OLE DB、 ADO的区别
一.ODBC ODBC的由来 1992年Microsoft和Sybase.Digital共同制定了ODBC标准接口,以单一的ODBC API来存取各种不同的数据库.随后ODBC便获得了许多数据库厂商和 ...
- c# 可以设置透明度的 Panel 组件
using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; u ...
- JDBC Driver
Infomix Driver: com.informix.jdbc.IfxDriver JDBC URL : jdbc:informix-sqli://<host>:<port> ...
- 修改tcp内核参数:somaxconn
修改somaxconn 该内核参数默认值一般是128(定义了系统中每一个端口最大的监听队列的长度),对于负载很大的服务程序来说大大的不够.一般会将它修改为2048或者更大. echo 2048 > ...
- Tortoise SVN Clean up失败的解决方法
step1: 到 sqlite官网 (http://www.sqlite.org/download.html) 下载 sqlite3.exe (找到 Precompiled Binaries for ...
- C#中的 int?是什么意思
http://www.cnblogs.com/firstcsharp/archive/2011/12/11/2283797.html int?:表示可空类型,就是一种特殊的值类型,它的值可以为null ...