LeetCode_232-Implement Queue using Stacks
题意是使用栈实现队列;队列是先进先出,后进后出。
class MyQueue {
public:
/** Initialize your data structure here. */
MyQueue() {
//intput
m_bSwap = true;
}
/** Push element x to the back of queue. */
void push(int x) {
if(!m_bSwap) {
while(!m_soutput.empty()) {
m_sintput.push(m_soutput.top());
m_soutput.pop();
}
m_bSwap = true;
}
m_sintput.push(x);
}
/** Removes the element from in front of queue and returns that element. */
int pop() {
if(m_bSwap) {
while(!m_sintput.empty()) {
m_soutput.push(m_sintput.top());
m_sintput.pop();
}
m_bSwap = false;
}
int nNum = m_soutput.top();
m_soutput.pop();
return nNum;
}
/** Get the front element. */
int peek() {
if(m_bSwap) {
while(!m_sintput.empty()) {
m_soutput.push(m_sintput.top());
m_sintput.pop();
}
m_bSwap = false;
}
return m_soutput.top();
}
/** Returns whether the queue is empty. */
bool empty() {
if(m_sintput.empty() && m_soutput.empty()) {
return true;
}
return false;
}
protected:
stack<int> m_sintput;
stack<int> m_soutput;
bool m_bSwap;
};

可关注公众号了解更多的面试技巧
LeetCode_232-Implement Queue using Stacks的更多相关文章
- leetcode:Implement Stack using Queues 与 Implement Queue using Stacks
一.Implement Stack using Queues Implement the following operations of a stack using queues. push(x) - ...
- 【LeetCode】232 & 225 - Implement Queue using Stacks & Implement Stack using Queues
232 - Implement Queue using Stacks Implement the following operations of a queue using stacks. push( ...
- 232. Implement Queue using Stacks,225. Implement Stack using Queues
232. Implement Queue using Stacks Total Accepted: 27024 Total Submissions: 79793 Difficulty: Easy Im ...
- 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 ...
- Lintcode: Implement Queue by Stacks 解题报告
Implement Queue by Stacks 原题链接 : http://lintcode.com/zh-cn/problem/implement-queue-by-stacks/# As th ...
- 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) 4
232. 用栈实现队列 232. Implement Queue using Stacks 题目描述 使用栈实现队列的下列操作: push(x) -- 将一个元素放入队列的尾部. pop() -- 从 ...
- [LeetCode] Implement Queue using Stacks 用栈来实现队列
Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...
- Leetcode 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 ...
随机推荐
- springboot使用Jpa连接数据库
springboot使用Jpa连接数据库 1.pom.xml: <?xml version="1.0" encoding="UTF-8"?> < ...
- android.intent.category.BROWSABLE
参考: http://blog.csdn.net/annkie/article/details/8349626 http://xiechengfa.iteye.com/blog/1004991 BRO ...
- Spring Boot 自定义 Banner 教程
我们在启动 SpringBoot 时,控制台会打印 SpringBoot Logo 以及版本信息.有的时候我们需要自己弄个有个性的文本图片.Spring Boot 为我们提供了自定义接口. . ___ ...
- (六十九)c#Winform自定义控件-垂直滚动条
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...
- charles 帮助菜单总结
本文参考:charles 帮助菜单总结 charles的window和help的菜单介绍 其中window菜单的如下 这里我一般用到的就是 Active connections:可以用它来看charl ...
- ansible-playbook流程控制-loops循环使用
1. ansible-playbook流程控制-loops循环使用 有时你想要多次重复任务.在计算机编程中,这称为循环.common ansible循环包括使用文件模块更改多个文件和/或目录的所 ...
- TestNG(三) 基本注解BeforeMethod和AfterMethod
package com.course.testng; import org.testng.annotations.*; public class BasicAnnotation { @Test //最 ...
- HTML文档简介
HTML简介 HTML标签 html文档标签: html源代码就好像word文档,有特殊的语法结构定义自己的功能. html文档标签 html标签,其下由两个主要节点标签head.body. head ...
- ActiveMQ高级特性
一.常用配置属性 以下配置文件目录均为:${activemq_home}/conf/activemq.xml 1.定期扫描清理 ActiveMQ中有一项功能:Delete Inactive Desti ...
- 搭建数据库galera集群
galera集群 galera简介 galera集群又叫多主集群,用于数据库的同步,保证数据安全 最少3台,最好是奇数台数,当一台机器宕掉时,因为仲裁机制,这台机器就会被踢出集群. 通过wsrep协议 ...