【LeetCode】225. Implement Stack using Queues
题目:
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
, andis 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).
提示:
此题要求用“队列”实现“栈”,我们可以通过将队列进行循环来实现栈的特性。比如在push()的时候,被push元素之前的所有元素都重新pop出来再push到队尾,经过这一操作之后,队列中的元素顺序就能和栈的“先进后出”的顺序一致了。
通过这种方法,push是O(n)的,而pop,top,empty都是O(1)的。
代码:
- class Stack {
- queue<int> q;
- public:
- // Push element x onto stack.
- void push(int x) {
- q.push(x);
- for (int i = ; i < q.size() - ; ++i) {
- q.push(q.front());
- q.pop();
- }
- }
- // Removes the element on top of the stack.
- void pop() {
- q.pop();
- }
- // Get the top element.
- int top() {
- return q.front();
- }
- // Return whether the stack is empty.
- bool empty() {
- return q.empty();
- }
- };
【LeetCode】225. Implement Stack using Queues的更多相关文章
- 【LeetCode】225. Implement Stack using Queues 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【一天一道LeetCode】#225. Implement Stack using Queues
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Impleme ...
- 【easy】225. Implement Stack using Queues
用队列实现栈.这个实现方法十分的简单,就是在push这一步的时候直接变成逆序. class MyStack { private: queue<int> q; queue<int> ...
- 【leetcode❤python】 225. Implement Stack using Queues
#-*- coding: UTF-8 -*-class Stack(object): def __init__(self): """ i ...
- 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 ...
- 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 ...
随机推荐
- PS不能存储,因为程序错误
当PS中遇到不能存储文件,因为程序错误时,可以这样: http://www.zcool.com.cn/article/ZMTgwOTQw.html
- 转:Centos6.5_x86安装Redis。
Redis是一个高性能的,开源key-value型数据库.是构建高性能,可扩展的Web应用的完美解决方案,可以内存存储亦可持久化存储.因为要使用跨进程,跨服务级别的数据缓存,在对比多个方案后,决定使用 ...
- Kafka学习-入门
在上一篇kafka简介的基础之上,本篇主要介绍如何快速的运行kafka. 在进行如下配置前,首先要启动Zookeeper. 配置单机kafka 1.进入kafka解压目录 2.启动kafka bin\ ...
- 每天一道Java题[4]
问题 怎么将字符串转换为int? 解答 此题看似简单,但经常出现在笔试等地方,由于大家习惯了用IDE,有什么还真未必能写出来.通常都是parseInt()方法进行转换,如下: Int n = Inte ...
- VR全景加盟-全景智慧城市携万千创业者决战BAT
在所谓互联网思维走到末路.可穿戴设备基本昙花一现的大环境下,很多互联网人员转战VR市场,自然喜欢用互联网思维来考虑.笔者认识一些投资界人士,在谈到投资时,他们经常就问以下几句话:2B还是2C?将来有多 ...
- JSP----获取表单参数
在页面中可大量使用 request 对象来获取表单域的值,获取表单域的值有如下两个 方法. • String getParamete(String para mN ame): 获取表单域的值. • S ...
- struts2.1.6教程四_2、ActionContext 、ValueStack 、Stack Context
ActionContext 一次Action调用都会创建一个ActionContext 调用:ActionContext context = ActionContext.getContext() Va ...
- Libevent源码分析—event_init()
下面开始看初始化event_base结构的相关函数.相关源码位于event.c event_init() 首先调用event_init()初始化event_base结构体 struct event_b ...
- Scheme 中的 pair 和 list 简述
pair (cons 1 2) > (1 . 2) 系统返回(1 . 2).cons 操作给两个地址分配了内存空间,并把存放指向 1 的地址放在一个空间,把存放指向2的地址放在另一个空间.存放指 ...
- a标签实现文件下载
如果想通过纯前端技术实现文件下载,直接把a标签的href属性设置为文件路径即可,如下: <a href="https://cdn.shopify.com/s/files/1/1545/ ...