【leetcode】622. Design Circular Queue
题目如下:
Design your implementation of the circular queue. The circular queue is a linear data structure in which the operations are performed based on FIFO (First In First Out) principle and the last position is connected back to the first position to make a circle. It is also called "Ring Buffer".
One of the benefits of the circular queue is that we can make use of the spaces in front of the queue. In a normal queue, once the queue becomes full, we cannot insert the next element even if there is a space in front of the queue. But using the circular queue, we can use the space to store new values.
Your implementation should support following operations:
MyCircularQueue(k)
: Constructor, set the size of the queue to be k.Front
: Get the front item from the queue. If the queue is empty, return -1.Rear
: Get the last item from the queue. If the queue is empty, return -1.enQueue(value)
: Insert an element into the circular queue. Return true if the operation is successful.deQueue()
: Delete an element from the circular queue. Return true if the operation is successful.isEmpty()
: Checks whether the circular queue is empty or not.isFull()
: Checks whether the circular queue is full or not.Example:
MyCircularQueue circularQueue = new MyCircularQueue(3); // set the size to be 3
circularQueue.enQueue(1); // return true
circularQueue.enQueue(2); // return true
circularQueue.enQueue(3); // return true
circularQueue.enQueue(4); // return false, the queue is full
circularQueue.Rear(); // return 3
circularQueue.isFull(); // return true
circularQueue.deQueue(); // return true
circularQueue.enQueue(4); // return true
circularQueue.Rear(); // return 4Note:
- All values will be in the range of [0, 1000].
- The number of operations will be in the range of [1, 1000].
- Please do not use the built-in Queue library.
解题思路:用list就能搞定了。
代码如下:
class MyCircularQueue(object): def __init__(self, k):
"""
Initialize your data structure here. Set the size of the queue to be k.
:type k: int
"""
self.capacity = k
self.l = [] def enQueue(self, value):
"""
Insert an element into the circular queue. Return true if the operation is successful.
:type value: int
:rtype: bool
"""
if len(self.l) >= self.capacity:
return False
self.l.append(value)
return True def deQueue(self):
"""
Delete an element from the circular queue. Return true if the operation is successful.
:rtype: bool
"""
if len(self.l) == 0:
return False
self.l.pop(0)
return True def Front(self):
"""
Get the front item from the queue.
:rtype: int
"""
return self.l[0] if len(self.l) > 0 else -1 def Rear(self):
"""
Get the last item from the queue.
:rtype: int
"""
return self.l[-1] if len(self.l) > 0 else -1 def isEmpty(self):
"""
Checks whether the circular queue is empty or not.
:rtype: bool
"""
return len(self.l) == 0 def isFull(self):
"""
Checks whether the circular queue is full or not.
:rtype: bool
"""
return len(self.l) == self.capacity
【leetcode】622. Design Circular Queue的更多相关文章
- 【LeetCode】622. Design Circular Queue 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 用直的代替弯的 数组循环利用 日期 题目地址:htt ...
- 【LeetCode】641. Design Circular Deque 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/design-ci ...
- LeetCode 622. Design Circular Queue
原题链接在这里:https://leetcode.com/problems/design-circular-queue/ 题目: Design your implementation of the c ...
- [LeetCode] 622.Design Circular Queue 设计环形队列
Design your implementation of the circular queue. The circular queue is a linear data structure in w ...
- 【LeetCode】1166. Design File System 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 目录树 日期 题目地址https://leetc ...
- 【LeetCode】355. Design Twitter 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】707. Design Linked List 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】706. Design HashMap 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】705. Design HashSet 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 位图法 数组法 日期 题目地址:https://le ...
随机推荐
- Struts2测试题
今天给大家看一套我最近做的一套关于Struts2的题: 1.以下关于jQuery说法错误的选项是( D ). A.“$”为jQuery脚本库的默认全局变量名,即“$” = “jQuery” B.$.a ...
- Linux下安装redis-4.0.10
1.下载redis-4.0.10 在redis官网(https://redis.io/download)下载redis-4.0.10 2.将安装包上传至Linux服务器 在Linux服务器根目录下创建 ...
- 2018.03.29 python-pandas transform/apply 的使用
#一般化的groupby方法:apply df = pd.DataFrame({'data1':np.random.rand(5), 'data2':np.random.rand(5), 'key1' ...
- Python子类调用父类内属性的方法
常见的就是初始化方法__init__() python中类的初始化方法是__init__(),因此父类子类的初始化方法都是这个,如果子类不实现这个函数,初始化时调用父类的初始化函数,如果子类实现这个函 ...
- 【HANA系列】SAP HANA SQL获取当前日期最后一天
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[HANA系列]SAP HANA SQL获取当前 ...
- http协议组成
摘要 本文主要内容介绍什么是http协议 了解http协议之前我们先了解一下当我们输入一个url到页面渲染出来,这个过程发什么了什么呢~ 网络开启线程开始解析url DNS解析域名,查询IP 建立TC ...
- Scratch少儿编程系列:(五)猜数字
一.程序说明: 程序生成1-100之间的随机数,如56,小明在输入框中输入猜的数字.如输入80,提示“猜大了”,如输入45,则提示“猜小了”. 不超过5次猜中,则成功,否则失败. 二.程序的流程图 为 ...
- tensorflow学习之tf.assign
tf.assign(ref, value, validate_shape=None, use_locking=None, name=None), 函数功能是将value赋值给ref ref必须是tf. ...
- 用seborn的函数distplot(), jointplot(), pairplt()对数据的单变量分析绘图
1.用seaborn的distplot()函数绘制直方图.参数kde = True时会把分布曲线也画出来. 如下代码所示是绘制标准正态分布的分布图 import seaborn as sns impo ...
- 网络编程.iocp
1.(20191212)查到的一些资料:java从 JDK7开始 引入AOI(即 NIO2).然后 实现 IOCP使用的是 AsynchronousChannelGroup.AsynchronousS ...