641. Design Circular Deque
1 题目理解
要求设计一个双端循环队列。这个队列能支持以下操作:
MyCircularDeque(k): Constructor, set the size of the deque to be k.
insertFront(): Adds an item at the front of Deque. Return true if the operation is successful.
insertLast(): Adds an item at the rear of Deque. Return true if the operation is successful.
deleteFront(): Deletes an item from the front of Deque. Return true if the operation is successful.
deleteLast(): Deletes an item from the rear of Deque. Return true if the operation is successful.
getFront(): Gets the front item from the Deque. If the deque is empty, return -1.
getRear(): Gets the last item from Deque. If the deque is empty, return -1.
isEmpty(): Checks whether Deque is empty or not.
isFull(): Checks whether Deque is full or not.
2 思路
再次来做的时候完全没有想法,参考了答案后回过头看自己的队列文章发现思路就在文章的循环队列里面讲明白了。
重点理解:
1 front表示第一个数字有效的位置, tail表示最有一个有效数字位置的下一位,也可以理解为要插入一个元素时候的位置。这两个变量的含义不能变。
2 队列为空的条件:front =tail
3 队列满的条件:(tail+1)%capacity=0,是要空出一个位置的。
class MyCircularDeque {private int capacity ;private int[] values;private int front;private int rear;/** Initialize your data structure here. Set the size of the deque to be k. */public MyCircularDeque(int k) {this.values = new int[k+1];this.capacity = k+1;}/** Adds an item at the front of Deque. Return true if the operation is successful. */public boolean insertFront(int value) {if(isFull()) return false;front = front == 0? capacity-1 :(front-1);values[front] = value;return true;}/** Adds an item at the rear of Deque. Return true if the operation is successful. */public boolean insertLast(int value) {if(isFull()) return false;values[rear] = value;rear = (rear+1)%capacity;return true;}/** Deletes an item from the front of Deque. Return true if the operation is successful. */public boolean deleteFront() {if(isEmpty()) return false;front = (front+1)%capacity;return true;}/** Deletes an item from the rear of Deque. Return true if the operation is successful. */public boolean deleteLast() {if(isEmpty()) return false;rear = rear==0? capacity-1: rear-1;return true;}/** Get the front item from the deque. */public int getFront() {return !isEmpty()?values[front]:-1;}/** Get the last item from the deque. */public int getRear() {return !isEmpty()?values[rear>0?rear-1:capacity-1]:-1;}/** Checks whether the circular deque is empty or not. */public boolean isEmpty() {return front == rear;}/** Checks whether the circular deque is full or not. */public boolean isFull() {return (rear+1)%capacity==front;}
}
641. Design Circular Deque相关推荐
- C#LeetCode刷题之#641-设计循环双端队列(Design Circular Deque)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4132 访问. 设计实现双端队列. 你的实现需要支持以下操作: M ...
- leetcode 622. Design Circular Queue | 641. 设计循环双端队列(Java)
题目 https://leetcode.com/problems/design-circular-deque/ 题解 相关问题:leetcode 622. Design Circular Queue ...
- socketmq 设置队列大小_LeetCode 622:设计循环队列 Design Circular Queue
LeetCode 622:设计循环队列 Design Circular Queue 首先来看看队列这种数据结构: 队列:先入先出的数据结构 在 FIFO 数据结构中,将首先处理添加到队列中的第一个元素 ...
- C#LeetCode刷题之#622-设计循环队列(Design Circular Queue)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4126 访问. 设计你的循环队列实现. 循环队列是一种线性数据结构 ...
- LeetCode 622. Design Circular Queue
LeetCode 622 循环队列,head== tail的时候作为空:留一个空位作为哨兵,当tail +1==head的时候我们标志为队列满了. class MyCircularQueue:def ...
- leetcode 622. Design Circular Queue | 622. 设计循环队列(Ring Buffer)
题目 https://leetcode.com/problems/design-circular-queue/ 题解 Ring Buffer 的实现,rear 指向新插入的位置,front 指向最旧的 ...
- 数据结构与算法总结(完结)
极客时间算法学习之后开始跟着花花酱刷题.大概从4月份开始的.从今天开始(2020-8-24)开始做总结,复习一下已经刷过的题目.到目前为止leetcode刷题323道. 2020/8/24 完成题目整 ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
原文地址:https://www.cnblogs.com/grandyang/p/4606334.html 终于将LeetCode的大部分题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开 ...
- C#LeetCode刷题-队列
队列篇 # 题名 刷题 通过率 难度 363 矩形区域不超过 K 的最大数值和 27.2% 困难 621 任务调度器 40.9% 中等 622 设计循环队列 C#LeetCode刷题之#622-设计循 ...
最新文章
- Python设计模式面向对象编程
- 网络协议 19 - RPC 协议:远在天边近在眼前
- boost::units模块实现测试显式和隐式单位转换
- King Gym - 102471H
- Git如何处理代码冲突
- BootStrap中Affix控件的使用方法及如何保持布局的美观
- python3基础(九)内置函数
- java Context类
- sqoop-1.4.7安装
- 【Android 12 AOSP学习】Android 12源码下载编译
- IT行业都有哪些职位,初学者如何选择才能够快速进入这个行业?
- 操作系统中的基础抽象
- 应用概率统计(陈魁)第十一章(回归分析)部分课后答案
- 腾讯面试Android开发
- Firefox插件的安装及使用方法(持续更新中)
- Birds in Forest
- 不靠体育赛事,咪咕视频还有多大发展空间?
- Android之Manifest文件
- 《金税盘--发票开具、发票领购、发票安全存储、发票管理、身份认证和抄报税功能详解》
- vue耦合程度是什么意思