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(). 移除队首元 ...
随机推荐
- OpenCV ——IplImage应用解析
由于OpenCV主要针对的是计算机视觉方面的处理,因此在函数库中,最重要的结构体是IplImage结构.IplImage结构来源于Intel的另外一个函数库Intel Image Processing ...
- [USACO15OPEN]回文的路径Palindromic Paths
[USACO15OPEN]回文的路径Palindromic Paths 题目描述 Farmer John's farm is in the shape of an N \times NN×N grid ...
- C# 加密、解密函数
#region ========加密======== /// <summary> /// 加密 /// </summary> /// <param name=" ...
- Java面向对象(接口、多态)
面向对象 今日内容介绍 u 接口 u 多态 第1章 接口 1.1 接口概念 接口是功能的集合,同样可看做是一种数据类型,是比抽象类更为抽象的”类”. 接口只描述所应该具备的方法,并没有具体实现,具体的 ...
- java http的get,post请求
初学可用F12查看任意网页帮助理解 package httpTest: import java.io.BufferedReader; import java.io.IOException;import ...
- AngularJS(五):表单及输入验证
本文也同步发表在我的公众号“我的天空” 表单基础 表单是HTML中很重要的一个部分,基本上我们的信息录入都依靠表单,接下来我们学习如何在AngularJS中使用表单,首先看以下示例代码: <bo ...
- Memcache笔记02-telnet操作memcached
telnet操作Memcached 登录到telnet连接到memcached服务: telnet 127.0.0.1 11211 memcached的基本命令: //当telnet登录成功可以看到一 ...
- Kail安装后的配置
安装完Kail系统后进行简单的几项配置可以让使用更方便 VMware安装Kail系统这里就不介绍了,大家可以参考这个博客:http://www.cnblogs.com/xuanhun/p/568831 ...
- 【Python图像特征的音乐序列生成】关于图像特征的描述词
查阅了很久的资料,决定依据Yoshida的<Image retrieval system using impression words>这篇论文里的词语来定义. Yoshida 等的 Ar ...
- DIV在另一个DIV里面垂直居中,水平居中
HTML: <div class="parent"> <div class="children"> <div class=&quo ...