【一天一道LeetCode】#232. Implement Queue using Stacks
一天一道LeetCode
本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github
欢迎大家关注我的新浪微博,我的新浪微博
欢迎转载,转载请注明出处
(一)题目
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)
(二)解题
题目大意:用两个栈实现queue,要求实现push,pop,peek和empty
解题思路:栈是先进后出,队列是先进先出。利用两个栈stack1和stack2
push:和栈一样,直接push到一个栈stack1中
pop:有一个辅助栈就可以将stack1中的数依次push到stack2中,这样stack2的top就是最先进来的数,直接pop即可
peek:去队首的数,如果stack2不为空的话,直接返回stack2.pop即可,如果stack2为空,则先把stack1的数push过来,再去top元素
empty:如果stack1和stack2均为空,就直接返回true,否则返回false
具体实现如下:
class Queue {
public:
// Push element x to the back of queue.
void push(int x) {
st1.push(x);
}
// Removes the element from in front of queue.
void pop(void) {
if(empty()) return;
if(st2.empty())
{
while(!st1.empty()){//为空的话要把stack1的元素push进来
st2.push(st1.top());
st1.pop();
}
}
st2.pop();
}
// Get the front element.
int peek(void) {
if(st2.empty()){//为空的话要把stack1的元素push进来
while(!st1.empty()){
st2.push(st1.top());
st1.pop();
}
}
return st2.top();
}
// Return whether the queue is empty.
bool empty(void) {
if(st1.empty()&&st2.empty()) return true;
else return false;
}
private:
stack<int> st1;
stack<int> st2;
};
【一天一道LeetCode】#232. Implement Queue using Stacks的更多相关文章
- 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 用栈来实现队列
Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...
- (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 ...
- Java [Leetcode 232]Implement Queue using Stacks
题目描述: Implement the following operations of a queue using stacks. push(x) -- Push element x to the b ...
- LeetCode 232 Implement Queue using Stacks
Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...
- Java for LeetCode 232 Implement Queue using Stacks
Stack<Integer> stack=new Stack<Integer>(); public void push(int x) { stack.push(x); } // ...
- Leetcode 232 Implement Queue using Stacks STL
本题用两个栈实现队列,用栈的基本操作去实现队列的所有基本操作push(),pop(),peek()以及empty() sa作为输入栈,sb作为输出栈,将sa输入元素的反转过来放到sb中 push与sa ...
- LeetCode 232 Implement Queue using Stacks 两个栈实现队列
class MyQueue { public: /** Initialize your data structure here. */ MyQueue() { } /** Push element x ...
- 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 ...
- 232. Implement Queue using Stacks,225. Implement Stack using Queues
232. Implement Queue using Stacks Total Accepted: 27024 Total Submissions: 79793 Difficulty: Easy Im ...
随机推荐
- Python Django rest framework
本节内容 Django rest framework 安装 Django rest framwwork 环境配置 简单举例说明 Django中使用 rest framework 1.1 安装 Djan ...
- Linked List Cycle II--寻找单链表中环的起始点
题目要求 Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull. ...
- Linux学习之CentOS(八)----详解文件的搜寻、查找(转)
which (寻找『运行档』) [root@www ~]# which [-a] command 选项或参数: -a :将所有由 PATH 目录中可以找到的命令均列出,而不止第一个被找到的命令名称 分 ...
- AQS简简单单过一遍
前言 回顾前面: 多线程三分钟就可以入个门了! Thread源码剖析 多线程基础必要知识点!看了学习多线程事半功倍 Java锁机制了解一下 只有光头才能变强! 本来我是打算在这章节中写Lock的子类实 ...
- 从 python 中 axis 参数直觉解释 到 CNN 中 BatchNorm 的工作方式(Keras代码示意)
1. python 中 axis 参数直觉解释 网络上的解释很多,有的还带图带箭头.但在高维下是画不出什么箭头的.这里阐述了 axis 参数最简洁的解释. 假设我们有矩阵a, 它的shape是(4, ...
- 迭代器&生成器
迭代器 迭代是Python最强大的功能之一,是访问集合元素的一种方式. 迭代器是一个可以记住遍历的位置的对象. 迭代器对象从集合的第一个元素开始访问,直到所有的元素被访问完结束.迭代器只能往前不会后退 ...
- compress函数用法详解
compress函数: 主要用来删除字符串中的特定字符. 1.compress函数的基本形式compress(<source><, chars><, modifiers& ...
- Hive中yyyymmdd和yyyy-mm-dd日期之间的切换
以2017-12-05和20171205相互转换为例说明 方法1: from_unixtime+ unix_timestamp --20171205转成2017-12-05 ','yyyymmdd') ...
- 进程间通信——XSI IPC之消息队列
进程间通信XSI IPC有3种:消息队列.共享内存.信号量.它们之间有很多相似之处,但也有各自的特殊的地方.消息队列作为其中比较简单的一种,它会有些什么东西呢,来一起探讨探讨.. 消息队列结构 消息队 ...
- javascript:void(0) 含义
javascript:void(0) 含义 我们经常会使用到 javascript:void(0) 这样的代码,那么在 JavaScript 中 javascript:void(0) 代表的是什么意思 ...