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 两个栈实现队列的更多相关文章

  1. Leetcode 232 Implement Queue using Stacks 和 231 Power of Two

    1. 232 Implement Queue using Stacks 1.1 问题描写叙述 使用栈模拟实现队列.模拟实现例如以下操作: push(x). 将元素x放入队尾. pop(). 移除队首元 ...

  2. Leetcode 232 Implement Queue using Stacks STL

    本题用两个栈实现队列,用栈的基本操作去实现队列的所有基本操作push(),pop(),peek()以及empty() sa作为输入栈,sb作为输出栈,将sa输入元素的反转过来放到sb中 push与sa ...

  3. Java [Leetcode 232]Implement Queue using Stacks

    题目描述: Implement the following operations of a queue using stacks. push(x) -- Push element x to the b ...

  4. LeetCode 232 Implement Queue using Stacks

    Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...

  5. [LeetCode] 232. Implement Queue using Stacks 用栈来实现队列

    Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...

  6. (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 ...

  7. Implement Queue using Stacks(用栈实现队列)

    Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...

  8. Java for LeetCode 232 Implement Queue using Stacks

    Stack<Integer> stack=new Stack<Integer>(); public void push(int x) { stack.push(x); } // ...

  9. 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 ...

随机推荐

  1. backbonejs学习

    文章: http://www.cnblogs.com/yexiaochai/archive/2013/07/27/3219402.html http://blog.csdn.net/cony100/a ...

  2. rsync同步文件,排除多个文件/目录

    使用rsync -aP --exclude=upload 只能排除upload文件/目录.如果要排除多个文件/目录,怎么办?  那只能建一个exclude.list,里面填写要排除的目录(一行一个文件 ...

  3. VS2008中宽字节和普通字节的使用

    由于麻烦,所以并没有使用宽字节,留待以后.

  4. Qt安装与配置

    安装Qt 安装Qt Creator,打开终端执行如下命令: sudo apt-get install qt5-default qtcreator -y 安装Qt示例和文档: sudo apt-get ...

  5. 超牛 猴子补丁,修改python内置的print

    猴子补丁一般是用于修改三方包或官方包,也可以用来修改自己或者他人的代码. 但也可以用来修改python 语言内置的关键字. 本篇博客修改python最常用的内置print,使你使用print时候,自动 ...

  6. .NET牛人养成计划

    六大喜讯:(1)对于小型平板等授权免费(2)编译平台Rosly开源,ASP.NET全系平台开源(ASP.NET,Web API):ASP.NET跨平台,Mono,让ASP.NET运行在Linux和Un ...

  7. 创建sharepoint网站

    1.首先打开管理中心 输入用户名和密码(默认是本机的管理员名称及密码) 在“应用程序管理”选择“管理WEB应用程序” 新建应用程序 选择一个没有占用的端口,选择允许匿名访问 数据库名称一般为WSS_C ...

  8. macos上改变输入法顺序

    设置界面上是不能拖放顺序的,唯一解决办法是: 一.先选择所有文档使用相同输入源 二.选择用美国英语 三.再选择允许多个输入源,再打开原来的中文输入法 顺序就调过来了!尼玛,这就是苹果的人性化?懒得吐嘈 ...

  9. NULL 与 ""

    char *str1 = NULL; //str1为空 char *str2 = ""; //str2为一个空字符串 NULL没有分配空间,""分配了空间.

  10. 服务器控件button点击时执行脚本弹出提示对话框Button2.Attributes.Add("onclick","事件")

    <HTML> <HEAD>  <title>**********资料更新</title>  <meta content="Microso ...