LeetCode 232 Implement Queue using Stacks 两个栈实现队列
class MyQueue {
public:
/** Initialize your data structure here. */
MyQueue() { } /** Push element x to the back of queue. */
void push(int x) {
stkPush.push(x);
} /** Removes the element from in front of queue and returns that element. */
int pop() {
int val;
if(stkPop.empty())
{
while(!stkPush.empty())
{
val=stkPush.top();
stkPush.pop();
stkPop.push(val);
}
}
val=stkPop.top();
stkPop.pop();
return val;
} /** Get the front element. */
int peek() {
if(stkPop.empty())
{
while(!stkPush.empty())
{
int val=stkPush.top();
stkPush.pop();
stkPop.push(val);
}
}
return stkPop.top();
} /** Returns whether the queue is empty. */
bool empty() {
return stkPush.empty()&&stkPop.empty();
}
private:
stack<int> stkPush;
stack<int> stkPop;
}; /**
* Your MyQueue object will be instantiated and called as such:
* MyQueue obj = new MyQueue();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.peek();
* bool param_4 = obj.empty();
*/
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 STL
本题用两个栈实现队列,用栈的基本操作去实现队列的所有基本操作push(),pop(),peek()以及empty() sa作为输入栈,sb作为输出栈,将sa输入元素的反转过来放到sb中 push与sa ...
- 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 ...
- [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 ...
- 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 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 ...
随机推荐
- Operating System-Kickoff:什么是操作系统&&操作系统的核心概念
接下来会写一系列Operating System(操作系统)的文章,今天先开个头.本文主要内容: 什么是操作系统 操作系统的核心概念 程序=数据结构+算法 一.什么是操作系统 1.1 操作系统是对硬件 ...
- Azure blob Storage Snapshot
用户虚拟机硬盘的备份是客户在部署云应用中是一个非常重要的部分. 目前有多种平台的备份方法: 捕获镜像:可以采用Capture的方式(powershell命令为Save-AzureVMImage)捕获虚 ...
- 【转】mysql查询当天所有数据sql语句
mysql查询当天的所有信息: select * from test where year(regdate)=year(now()) and month(regdate)=month(now()) a ...
- Python:format()方法
转于:https://blog.csdn.net/zhang89xiao/article/details/53818906 博主:张肖的博客 描述: format的格式 replacement_fie ...
- 【转】 Pro Android学习笔记(五九):Preferences(3):EditText和Ringtone Preference
目录(?)[-] EditText Preferences xml文件 设备的存贮文件 Ringtone Preferences EditText Preferences xml文件 在res/xml ...
- Java 的 Tuple 元组数据类型
元组类型,即 Tuple 常在脚本语言中出现,例如 Scala 的 ("Unmi", "china@qq.com", "blahbla"). ...
- kafka之一:Windows上搭建Kafka运行环境
搭建环境 1. 安装JDK 1.1 安装文件:http://www.oracle.com/technetwork/java/javase/downloads/jre8-downloads-213315 ...
- MAXOS安装FFMPEG
安装brew 安装方法:命令行输入 /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/ins ...
- CSS是什么?W3C是什么?W3C盒子与IE盒子的区别?
(1)层叠样式(Cascading Style Sheets, CSS)是用来表现HTML或XML文本样式的语言. (2)W3C推荐规范(World Wide Web Consortium,W3C ...
- [matlab]Monte Carlo模拟学习笔记
理论基础:大数定理,当频数足够多时,频率可以逼近概率,从而依靠概率与$\pi$的关系,求出$\pi$ 所以,rand在Monte Carlo中是必不可少的,必须保证测试数据的随机性. 用蒙特卡洛方法进 ...