LeetCode(232) Implement Queue using Stacks
题目
Implement the following operations of a queue using stacks.
push(x) – Push element x to the back of queue.
pop() – Removes the element from in front of queue.
peek() – Get the front element.
empty() – Return whether the queue is empty.
Notes:
You must use only standard operations of a stack – which means only push to top, peek/pop from top, size, and is empty operations are valid.
Depending on your language, stack may not be supported natively. You may simulate a stack by using a list or deque (double-ended queue), as long as you use only standard operations of a stack.
You may assume that all operations are valid (for example, no pop or peek operations will be called on an empty queue).
分析
用栈实现队列;
只需要两个辅助栈,一个保存压入元素,一个保存弹出元素;当弹出元素栈空时,把所有入栈元素压入出栈;
AC代码
class Queue {
public:
// Push element x to the back of queue.
void push(int x) {
inStack.push(x);
}
// Removes the element from in front of queue.
void pop(void) {
if (outStack.empty())
{
while (!inStack.empty())
{
outStack.push(inStack.top());
inStack.pop();
}
}
outStack.pop();
}
// Get the front element.
int peek(void) {
if (outStack.empty())
{
while (!inStack.empty())
{
outStack.push(inStack.top());
inStack.pop();
}
}
return outStack.top();
}
// Return whether the queue is empty.
bool empty(void) {
if (outStack.empty() && inStack.empty())
return true;
return false;
}
private:
stack<int> inStack;
stack<int> outStack;
};
LeetCode(232) Implement Queue using Stacks的更多相关文章
- 【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( ...
- 【LeetCode OJ 232】Implement Queue using Stacks
题目链接:https://leetcode.com/problems/implement-queue-using-stacks/ 题目:Implement the following operatio ...
- LeetCode 232:Implement Queue using Stacks
Implement the following operations of a queue using stacks. push(x) -- Push element x to the back ...
- 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(28)Implement strStr()
题目 Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if nee ...
- LeetCode(225) Implement Stack using Queues
题目 Implement the following operations of a stack using queues. push(x) – Push element x onto stack. ...
- LeetCode 232. 用栈实现队列(Implement Queue using Stacks) 4
232. 用栈实现队列 232. Implement Queue using Stacks 题目描述 使用栈实现队列的下列操作: push(x) -- 将一个元素放入队列的尾部. pop() -- 从 ...
- 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 ...
- Leetcode 232 Implement Queue using Stacks 和 231 Power of Two
1. 232 Implement Queue using Stacks 1.1 问题描写叙述 使用栈模拟实现队列.模拟实现例如以下操作: push(x). 将元素x放入队尾. pop(). 移除队首元 ...
随机推荐
- python实现批量远程执行命令及批量上传下载文件
#!/usr/bin/env python # -*- coding: utf- -*- # @Time : // : # @Author : xuxuedong # @Site : # @File ...
- 牛客网Java刷题知识点之为什么static成员方法不能是抽象方法,其必须实现
不多说,直接上干货! static修饰的方法我们称之为静态方法,我们通过类名对其进行直接调用.由于它在类加载的时候就存在了,它不依赖于任何实例,所以static方法必须实现,也就是说它不能是抽象方法.
- untiy3d开发环境搭建和开发准备
1.到untiy3d官网上下载untiy3d的软件(这里我使用的是个人版的5.3.61f做学习使用) 2.安装vs2012 3.因为unity3d和vs的版本有一定的版本对应关系,我这里使用vs201 ...
- 洛谷P1081 开车旅行70分
https://www.luogu.org/problem/show?pid=1081 太遗憾了明明写出来了,却把最小值初始值弄小了,从第二个点开始就不可能对了.70分! #include<io ...
- (wp8.1)样式资源
在开发wp的时候,难免要设置好多属性.但是有好多属性是重复的,我们可不可统一 设置样式呢,答案是肯定的 代码如下 <Page x:Class="Blue.MainPage" ...
- Spring Boot自动配置原理与实践(一)
前言 Spring Boot众所周知是为了简化Spring的配置,省去XML的复杂化配置(虽然Spring官方推荐也使用Java配置)采用Java+Annotation方式配置.如下几个问题是我刚开始 ...
- uvm_mem_mam——寄存器模型(十三)
有了存储器模型,再来看看存储器的管理 //------------------------------------------------------------------------------ ...
- PHP实现正态分布的累积概率函数
在实际项目中,遇到需要正态分布算法去计算一个数值在整体的分布区间,例如: 100,90,80,70,60,50,40,30,20,10共10个数,按从高到低的顺序排序,总数的10%分布区域为极高频, ...
- SQL Server 2017的Linked Server配置触发的bug“Exception Code = c0000005 EXCEPTION_ACCESS_VIOLATION”
SQL Server 2017的Linked Server配置触发的bug"Exception Code = c0000005 EXCEPTION_ACCESS_VIOLATION&q ...
- img标签src资源无法加载,报net::ERR_RESPONSE_HEADERS_MULTIPLE_CONTENT_DISPOSITION错
html代码: chrome和360浏览器均报错,系统自带IE.Firefox浏览器没有问题 原因:加载的资源名含有半角逗号(,)或者别的特殊符号 解决办法:后台给资源名加上双引号("&qu ...