问题描述:

使用队列实现栈的下列操作:

  • push(x) -- 元素 x 入栈
  • pop() -- 移除栈顶元素
  • top() -- 获取栈顶元素
  • empty() -- 返回栈是否为空

注意:

  • 你只能使用队列的基本操作-- 也就是 push to back, peek/pop from front, size, 和 is empty 这些操作是合法的。
  • 你所使用的语言也许不支持队列。 你可以使用 list 或者 deque(双端队列)来模拟一个队列 , 只要是标准的队列操作即可。
  • 你可以假设所有操作都是有效的(例如, 对一个空的栈不会调用 pop 或者 top 操作)。

方法:

 class MyStack(object):

     def __init__(self):
"""
Initialize your data structure here.
"""
self.lists = [] def push(self, x):
"""
Push element x onto stack.
:type x: int
:rtype: void
"""
self.lists.append(x) def pop(self):
"""
Removes the element on top of the stack and returns that element.
:rtype: int
"""
if len(self.lists) == 0:
return
return self.lists.pop() def top(self):
"""
Get the top element.
:rtype: int
"""
return self.lists[-1] def empty(self):
"""
Returns whether the stack is empty.
:rtype: bool
"""
return len(self.lists) == 0

2018-09-19 15:07:25

LeetCode--225--用队列实现栈的更多相关文章

  1. Java实现 LeetCode 225 用队列实现栈

    225. 用队列实现栈 使用队列实现栈的下列操作: push(x) – 元素 x 入栈 pop() – 移除栈顶元素 top() – 获取栈顶元素 empty() – 返回栈是否为空 注意: 你只能使 ...

  2. [Leetcode]225. 用队列实现栈 、剑指 Offer 09. 用两个栈实现队列

    ##225. 用队列实现栈 如题 ###题解 在push时候搞点事情:push时入队1,在把队2的元素一个个入队1,再交换队2和队1,保持队1除pushguocheng 始终为空. ###代码 cla ...

  3. Leetcode 春季打卡活动 第一题:225. 用队列实现栈

    Leetcode 春季打卡活动 第一题:225. 用队列实现栈 Leetcode 春季打卡活动 第一题:225. 用队列实现栈 解题思路 这里用了非常简单的思路,就是在push函数上做点操作,让队头总 ...

  4. 领扣(LeetCode)用队列实现栈 个人题解

    使用队列实现栈的下列操作: push(x) -- 元素 x 入栈 pop() -- 移除栈顶元素 top() -- 获取栈顶元素 empty() -- 返回栈是否为空 注意: 你只能使用队列的基本操作 ...

  5. [LeetCode] 226. 用队列实现栈

    题目链接: https://leetcode-cn.com/problems/implement-stack-using-queues 难度:简单 通过率:59.9% 题目描述: 使用队列实现栈的下列 ...

  6. [LeetCode] 225. Implement Stack using Queues 用队列来实现栈

    Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...

  7. LeetCode 225 Implement Stack using Queues 用队列实现栈

    1.两个队列实现,始终保持一个队列为空即可 class MyStack { public: /** Initialize your data structure here. */ MyStack() ...

  8. LeetCode 225:用队列实现栈 Implement Stack using Queues

    题目: 使用队列实现栈的下列操作: push(x) -- 元素 x 入栈 pop() -- 移除栈顶元素 top() -- 获取栈顶元素 empty() -- 返回栈是否为空 Implement th ...

  9. 【LeetCode题解】225_用队列实现栈(Implement-Stack-using-Queues)

    目录 描述 解法一:双队列,入快出慢 思路 入栈(push) 出栈(pop) 查看栈顶元素(peek) 是否为空(empty) Java 实现 Python 实现 解法二:双队列,入慢出快 思路 入栈 ...

  10. 225 Implement Stack using Queues 队列实现栈

    Implement the following operations of a stack using queues. push(x) -- Push element x onto stack.pop ...

随机推荐

  1. Java设计模式应用——模板方法模式

    所谓模板方法模式,就是在一组方法结构一致,只有部分逻辑不一样时,使用抽象类制作一个逻辑模板,具体是实现类仅仅实现特殊逻辑就行了.类似科举制度八股文,文章结构相同,仅仅具体语句有差异,我们只需要按照八股 ...

  2. charles破解

    替换安装路径->Charles\lib下的charles.jar文件成破解版jar文件,如果再次启动未弹出30天试用的提示,说明破解成功 charles:https://pan.baidu.co ...

  3. python进程、多进程

    进程: 进程(Process)是计算机中的程序关于某数据集合上的一次运行活动,是系统进行资源分配和调度的基本单位,是操作系统结构的基础.在早期面向进程设计的计算机结构中,进程是程序的基本执行实体:在当 ...

  4. mysql删除有外链索引数据,Cannot delete or update a parent row: a foreign key constraint fails 问题的解决办法

    mysql删除有外链索引数据Cannot delete or update a parent row: a foreign key constraint fails 问题的解决办法查询:DELETE ...

  5. Rest风格(占位符)的映射

  6. Kafka学习之(六)搭建kafka集群

    想要搭建kafka集群,必须具备zookeeper集群,关于zookeeper集群的搭建,在Kafka学习之(五)搭建kafka集群之Zookeeper集群搭建博客有说明.需要具备两台以上装有zook ...

  7. j2ee分布式缓存同步实现方案dlcache v1.0.1

    j2ee分布式缓存同步实现方案dlcache v1.0.1 发布 修复问题: 1.支持两个层次的缓存,典型的用于产品大类.产品小类,数据字典以及子项: 更新后见: pan http://pan.bai ...

  8. Hive-复制表

    非分区表复制 复制一张非分区表,使用CREATE TABLE IF NOT EXISTS AS SELECT * FROM tb_name;只复制表结构,CREATE TABLE IF NOT EXI ...

  9. xshell的Solarized Dark配色方案

    之前在ubuntu, kali, mint, air下都使用这一款配色方案,后来在网上看到有人在xshell中使用,配色方案有分享,就是一起无法导入 原来这个东西在你现有的连接无法直接导入,需要重新打 ...

  10. linux下通过命令行上传文件到百度网盘

    一.环境: centos release 6.9 python 2.7.13 二.安装工具bypy sudo pip install bypy 三.使用bypy 3.1 授权 [root@ineedl ...