本题用两个栈实现队列,用栈的基本操作去实现队列的所有基本操作push(),pop(),peek()以及empty()

sa作为输入栈,sb作为输出栈,将sa输入元素的反转过来放到sb中

push与sa有关,而pop(),peek()与sb有关,即将sa输入元素出栈放到sb中(函数move).

为此,只有两个栈为空才能让队列为空

 class Queue {
public:
// Push element x to the back of queue.
stack<int> sa;
stack<int> sb;
void move(){
while(!sa.empty()){
sb.push(sa.top());
sa.pop();
}
}
void push(int x) {
sa.push(x);
} // Removes the element from in front of queue.
void pop(void) {
if(!sb.empty()) sb.pop();
else{
move();
sb.pop();
}
} // Get the front element.
int peek(void) {
if(!sb.empty()) return sb.top();
else{
move();
return sb.top();
} } // Return whether the queue is empty.
bool empty(void) {
return sa.empty() && sb.empty();
}
};

Leetcode 232 Implement Queue using Stacks STL的更多相关文章

  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 用栈来实现队列

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

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

  4. Java [Leetcode 232]Implement Queue using Stacks

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

  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. Java for LeetCode 232 Implement Queue using Stacks

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

  7. LeetCode 232 Implement Queue using Stacks 两个栈实现队列

    class MyQueue { public: /** Initialize your data structure here. */ MyQueue() { } /** Push element x ...

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

  9. 232. Implement Queue using Stacks,225. Implement Stack using Queues

    232. Implement Queue using Stacks Total Accepted: 27024 Total Submissions: 79793 Difficulty: Easy Im ...

随机推荐

  1. Selenium2+python自动化14-iframe

    前言 有很多小伙伴在拿163作为登录案例的时候,发现不管怎么定位都无法定位到,到底是什么鬼呢,本篇详细介绍iframe相关的切换 以http://mail.163.com/登录页面10为案例,详细介绍 ...

  2. python-->基础-->001-->基本模块使用汇总

    一.python调用linux系统命令模块 import os import commands 例如,调用系统命令执行ping操作: 使用commands模块方法:>>> impor ...

  3. mysql 4种启动方式

    mysql 4种启动方式 都是去调用mysqld文件 1. mysqld 启动 进入mysqld文件所在目录(/../libexec/mysqld) ./mysqld --defaults-file= ...

  4. 【转】sudo命令情景分析

    文章转自:http://www.cnblogs.com/hazir/p/sudo_command.html Linux 下使用 sudo 命令,可以让普通用户也能执行一些或者全部的 root 命令.本 ...

  5. servers中添加server时,看不到运行环境的选择。

    servers中添加server时,看不到运行环境的选择. 主要原因是tomcat目录中的配置文件格式不对.

  6. C# 基础(3)

    跳转语句 Goto(现在一般不使用) 标志位 Flag true false 常量:(不可改变的量) 语法: Const 类型 变量名 = 常量值 在定义时赋值,其他地方不能赋值 枚举 是自己定义了一 ...

  7. yum 安装 phpmyadmin

    1.安装apache yum -y install httpd httpd-devel 2.安装phpmyadmin yum -y -install phpmyadmin 3.配置phpmyadmin ...

  8. JS实现滑动门效果

    html部分 p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 31.0px Consolas; color: #2b7ec3 } p.p2 { margin ...

  9. strace 监控所有php-fpm worker

    strace命令详解  http://linux.die.net/man/1/strace strace -tt -T $(pidof 'php-fpm: pool www' | sed 's/\([ ...

  10. 找出单链表的倒数第K个(从1开始计数)结点的值

    typedef struct Link { int data; struct Link* next; }NODE,*pNODE; NODE *searchK(NODE *phead, int k) { ...