LeetCode--225--用队列实现栈
问题描述:
使用队列实现栈的下列操作:
- 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--用队列实现栈的更多相关文章
- Java实现 LeetCode 225 用队列实现栈
225. 用队列实现栈 使用队列实现栈的下列操作: push(x) – 元素 x 入栈 pop() – 移除栈顶元素 top() – 获取栈顶元素 empty() – 返回栈是否为空 注意: 你只能使 ...
- [Leetcode]225. 用队列实现栈 、剑指 Offer 09. 用两个栈实现队列
##225. 用队列实现栈 如题 ###题解 在push时候搞点事情:push时入队1,在把队2的元素一个个入队1,再交换队2和队1,保持队1除pushguocheng 始终为空. ###代码 cla ...
- Leetcode 春季打卡活动 第一题:225. 用队列实现栈
Leetcode 春季打卡活动 第一题:225. 用队列实现栈 Leetcode 春季打卡活动 第一题:225. 用队列实现栈 解题思路 这里用了非常简单的思路,就是在push函数上做点操作,让队头总 ...
- 领扣(LeetCode)用队列实现栈 个人题解
使用队列实现栈的下列操作: push(x) -- 元素 x 入栈 pop() -- 移除栈顶元素 top() -- 获取栈顶元素 empty() -- 返回栈是否为空 注意: 你只能使用队列的基本操作 ...
- [LeetCode] 226. 用队列实现栈
题目链接: https://leetcode-cn.com/problems/implement-stack-using-queues 难度:简单 通过率:59.9% 题目描述: 使用队列实现栈的下列 ...
- [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 用队列实现栈
1.两个队列实现,始终保持一个队列为空即可 class MyStack { public: /** Initialize your data structure here. */ MyStack() ...
- LeetCode 225:用队列实现栈 Implement Stack using Queues
题目: 使用队列实现栈的下列操作: push(x) -- 元素 x 入栈 pop() -- 移除栈顶元素 top() -- 获取栈顶元素 empty() -- 返回栈是否为空 Implement th ...
- 【LeetCode题解】225_用队列实现栈(Implement-Stack-using-Queues)
目录 描述 解法一:双队列,入快出慢 思路 入栈(push) 出栈(pop) 查看栈顶元素(peek) 是否为空(empty) Java 实现 Python 实现 解法二:双队列,入慢出快 思路 入栈 ...
- 225 Implement Stack using Queues 队列实现栈
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack.pop ...
随机推荐
- Learning to Rank算法介绍:RankNet,LambdaRank,LambdaMart
之前的博客:http://www.cnblogs.com/bentuwuying/p/6681943.html中简单介绍了Learning to Rank的基本原理,也讲到了Learning to R ...
- quartz-job实现定时任务配置
使用quartz开源调度框架,写服务实现在一些指定场景发送特定短信,创建一个实现org.quartz.Job接口的java类.Job接口包含唯一的方法: public void execute(Job ...
- Git本地仓库与远程github同步的时候提示fatal: remote origin already exists 错误解决办法
Git本地仓库与远程github同步的时候提示fatal: remote origin already exists 错误解决办法 1.git在本地的电脑创建了仓库,要远程同步github的仓库.使用 ...
- Linux基础命令---mkfs
mkfs 在磁盘分区上创建ext2.ext3.ext4.ms-dos.vfat文件系统,默认情况下会创建ext2.mkfs用于在设备上构建Linux文件系统,通常是硬盘分区.文件要么是设备名称(例如/ ...
- MySQL数据库总结
引擎 查看MySQL默认引擎:show variables like '%storage_engine%'; 查看表引擎:show table status from 数据库名; 修改表引擎alter ...
- Python入门之os.walk()方法
os.walk方法,主要用来遍历一个目录内各个子目录和子文件. os.walk(top, topdown=True, onerror=None, followlinks=False) 可以得到一个三元 ...
- c++学习之map基本操作
map作为最常用的数据结构之一,用的好可以大幅度的提升性能. // java_cpp_perftest.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h& ...
- noip2015 day1
不解释,很简单,直接按照题目的方法构造就行了 Code #include<iostream> #include<cstdio> #include<cctype> # ...
- poj Meteor Shower - 搜索
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16313 Accepted: 4291 Description Bess ...
- Python3基础 os.path.splitext 处理文件名,得到文件名+扩展名
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...