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 ...
随机推荐
- http之pragma
关于Pragma:no-cache,跟Cache-Control: no-cache相同.Pragma: no-cache兼容http 1.0 ,Cache-Control: no-cache是htt ...
- NSArray用法
//类方法初始化一个数组对象 [array count] : 得到这个数组对象的长度. [array objectAtIndex index]: 传入数组的索引(index) 得到数据对象. [arr ...
- POJ3126Prime Path(BFS)
#include"cstdio" #include"queue" #include"cstring" using namespace std ...
- JDBC初步
public class TestMySqlConnection{ public static void main(String[] args){ Class.forNa ...
- HTML5小知识
1.HTML5一种“妥协的”语法 2.特点: 标签不区分大小写 元素可以省略结束标签 元素属性可以省略属性值 属性的属性值可以不适用引号
- ansible应用案例-一键安装flask
一.添加主机 sudo vim /etc/ansible/hosts ------------------------------------------------------> [group ...
- IPC编程之共享内存
一,共享内存介绍 共享内存是三个IPC(Inter-Process Communication)机制中的一个,它允许两个不相关的进程访问同一个逻辑内存. 二.共享内存使用的函数 #include ...
- [dp]最长单调递增子序列LIS
https://www.51nod.com/tutorial/course.html#!courseId=12 解题关键: 如果将子序列按照长度由短到长排列,将他们的最大元素放在一起,形成新序列$B\ ...
- error C2512: “HelloWorld”: 没有合适的默认构造函数可用
error C2512: "HelloWorld": 没有合适的默认构造函数可用 c++ newbie error C2512: no appropriate default co ...
- hbase-0.98.1-cdh5.1.0 完全分布式搭建
cdh版与0.98版的配置一样 1.环境 master:c1 slave:c2,c3 CentOS 6.5 x64 ,hadoop-2.3.0-cdh5.1.0,zookeeper-3.4.5-cdh ...