【一天一道LeetCode】#225. Implement Stack using Queues
一天一道LeetCode
本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github
欢迎大家关注我的新浪微博,我的新浪微博
欢迎转载,转载请注明出处
(一)题目
Implement the following operations of a stack using queues.
- push(x) – Push element x onto stack.
- pop() – Removes the element on top of the stack.
- top() – Get the top element.
- empty() – Return whether the stack is empty.
Notes:
- You must use only standard operations of a queue – which means only push to back, peek/pop from front, size, and is empty operations are valid.
- Depending on your language, queue may not be supported natively. You may simulate a queue by using a list or deque (double-ended queue), as long as you use only standard operations of a queue.
- You may assume that all operations are valid (for example, no pop or top operations will be called on an empty stack)
(二)解题
题目大意:用queue来实现一个stack。
解题思路:queue是先进先出,stack是先进后出。采用一个queue就能实现stack
push:直接压入到queue
pop:需要弹出queue的尾元素,可是queue只能弹出queue的头元素。这时候可以把queue的头元素弹出再压入queue,一直到尾元素弹出即可
top:直接利用queue的back()即可知道stack顶元素
empty:判断queue是否为空。
class Stack {
public:
// Push element x onto stack.
void push(int x) {
que.push(x);
}
// Removes the element on top of the stack.
void pop() {
int size = que.size();
for(int i = 0 ; i < size-1 ;i++){//queue的头元素弹出再压入,直到尾元素弹出
que.push(que.front());
que.pop();
}
que.pop();
}
// Get the top element.
int top() {
return que.back();
}
// Return whether the stack is empty.
bool empty() {
if(que.empty()) return true;
return false;
}
private:
queue<int> que;
};
【一天一道LeetCode】#225. Implement Stack using Queues的更多相关文章
- LeetCode 225 Implement Stack using Queues(用队列来实现栈)(*)
翻译 用队列来实现栈的例如以下操作. push(x) -- 将元素x加入进栈 pop() -- 从栈顶移除元素 top() -- 返回栈顶元素 empty() -- 返回栈是否为空 注意: 你必须使用 ...
- [LeetCode] 225. Implement Stack using Queues 用队列来实现栈
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- Java for LeetCode 225 Implement Stack using Queues
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- (easy)LeetCode 225.Implement Stack using Queues
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- Leetcode 225 Implement Stack using Queues
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- Java [Leetcode 225]Implement Stack using Queues
题目描述: Implement the following operations of a stack using queues. push(x) -- Push element x onto sta ...
- Leetcode 225 Implement Stack using Queues STL
用两个队列去实现栈,这里我使用了队列数组q[2],在所有的过程中保证一个队列是空的 push时插入到空的队列中,然后将队列中的元素移到另一个队列中 pop时从不空的队列中pop() peek时从不空的 ...
- LeetCode 225 Implement Stack using Queues 用队列实现栈
1.两个队列实现,始终保持一个队列为空即可 class MyStack { public: /** Initialize your data structure here. */ MyStack() ...
- 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 ...
随机推荐
- ●HDU 2871 Memory Control(Splay)
●赘述题目 四种操作: ○Reset:将整个内存序列清空. ○New a:在尽量靠左的位置新建一个长度为a的内存块,并输出改内存块起始位置.(各个内存块即使相邻也不会合并..) ○Free a:将a点 ...
- [Noi2015]荷马史诗
来自FallDream的博客,未经允许,请勿转载,谢谢. 追逐影子的人,自己就是影子. ——荷马 Allison 最近迷上了文学.她喜欢在一个慵懒的午后,细细地品上一杯卡布奇诺,静静地阅读她爱不释手的 ...
- [BZOJ]1085 骑士精神(SCOI2005)
这种鲜明的玄学风格很明显就是十几年前的题目. Description 在一个5×5的棋盘上有12个白色的骑士和12个黑色的骑士, 且有一个空位.在任何时候一个骑士都能按照骑士的走法(它可以走到和它横坐 ...
- Gradle学习之部署上传项目
原先在公司做项目时,写了一个简单的基于gradle部署项目的脚本,今天翻出来记录一下 一.build.gradle buildscript { ext { env = System.getProper ...
- C语言第二次作业 ,
一:修改错题 1输出带框文字:在屏幕上输出以下3行信息. 将源代码输入编译器 运行程序发现错误 错误信息1: 错误原因:将stido.h拼写错误 改正方法:将stido.h改为stdio.h 错误信息 ...
- MySQL 内连接与外连接
1.内连接 MySQL中,join,cross join,inner join 是等价的. 2.外连接 2.1 左外连接 left join 2.2 右外连接 right join 3.连接条件 使 ...
- js去除空格,判断是否包含
js去除空格 function trimStr(str){ return str.replace(/(^\s*)|(\s*$)/g,""); } js判断是否包含 //是否包含 f ...
- RxSwift 系列(七) -- Connectable Operators
前言 本篇文章将要学习RxSwift中连接操作符. Connectable Observable在订阅时不发射事件消息,而是仅当调用它们的connect()方法时才发射消息,这样就可以等待所有我们想要 ...
- easyui datagrid 横向滚动条
要出现横向滚动条则不能有fitColumns:true/false
- 初识Redis系列之二:安装及简单使用
仅介绍windows下的安装 一:下载地址:https://github.com/MSOpenTech/redis/releases. Redis 支持 32 位和 64 位.这个需要根据你系统平台的 ...