【一天一道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 ...
随机推荐
- Linux下安装3.0以上的python
Linux下自带的python2.7是不建议删除的,很多系统软件依赖python2.7,但是现在我们学习python一般需要python3.0,下面介绍安装python3.0. 1.进入python官 ...
- php+xdebug+dbgp远程调试(多人)
目录 创建 DBGP 服务 配置 调试 创建 DBGP 服务 到 下载页面,下载 python 版本的 dbgp 到服务器上. 解压后执行 pydbgpproxy. 如果提示找不到 dbgp 模块,则 ...
- App上架应用市场,如何攻破安全过检难题
App的安全过检与众所熟知的安全检测是两个完全不同的概念.首先App行业本身对App安全过检有一定的要求与规范,其次2017年6月1日正式实施的<中国网络安全法>中就曾要求App在渠道上线 ...
- 将jdbc连接明文密码加密方案
最近没有及时写文章,把最近处理的几个问题集中了一下写出来.这篇文章是关于如何处理spring项目中引入数据库连接等 使用的用户名和密码的明文进行加密.防止被他人窃取利用. 我们选择的加密方式为DES加 ...
- 关于使用Git的几点小技巧
告诉git忽略对已经纳入版本管理的文件a的修改,git会一直忽略此文件直到重新告诉git可以再次跟踪此文件: git update-index --assume-unchanged a 告诉git恢复 ...
- 【博弈】【HDU】取石子游戏
取石子游戏 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- Bootstrap3 栅格系统-实例:响应列重置(Responsive column resets)
四层的网格你肯定会遇到问题,可用在特定的断点,你的列不清楚作为一个比另一个高完全正确.为了解决这个问题,结合使用.clearfix和响应的实用工具类. <div class="row& ...
- Programming In Scala笔记-第十一章、Scala中的类继承关系
本章主要从整体层面了解Scala中的类层级关系. 一.Scala的类层级 在Java中Object类是所有类的最终父类,其他所有类都直接或间接的继承了Object类.在Scala中所有类的最终父类为A ...
- Django extra 和 annotate
>>> qs=Question.objects.extra(select={'anum': 'SELECT COUNT(*) FROM questions_answer WHERE ...
- 【安卓开发】为什么不能往Android的Application对象里存储数据
在一个App里面总有一些数据需要在多个地方用到.这些数据可能是一个 session token,一次费时计算的结果等.通常为了避免activity之间传递对象的开销 ,这些数据一般都会保存到持久化存储 ...