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相关推荐

  1. C#LeetCode刷题之#641-设计循环双端队列(Design Circular Deque)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4132 访问. 设计实现双端队列. 你的实现需要支持以下操作: M ...

  2. leetcode 622. Design Circular Queue | 641. 设计循环双端队列(Java)

    题目 https://leetcode.com/problems/design-circular-deque/ 题解 相关问题:leetcode 622. Design Circular Queue ...

  3. socketmq 设置队列大小_LeetCode 622:设计循环队列 Design Circular Queue

    LeetCode 622:设计循环队列 Design Circular Queue 首先来看看队列这种数据结构: 队列:先入先出的数据结构 在 FIFO 数据结构中,将首先处理添加到队列中的第一个元素 ...

  4. C#LeetCode刷题之#622-设计循环队列​​​​​​​(Design Circular Queue)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4126 访问. 设计你的循环队列实现. 循环队列是一种线性数据结构 ...

  5. LeetCode 622. Design Circular Queue

    LeetCode 622 循环队列,head== tail的时候作为空:留一个空位作为哨兵,当tail +1==head的时候我们标志为队列满了. class MyCircularQueue:def ...

  6. leetcode 622. Design Circular Queue | 622. 设计循环队列(Ring Buffer)

    题目 https://leetcode.com/problems/design-circular-queue/ 题解 Ring Buffer 的实现,rear 指向新插入的位置,front 指向最旧的 ...

  7. 数据结构与算法总结(完结)

    极客时间算法学习之后开始跟着花花酱刷题.大概从4月份开始的.从今天开始(2020-8-24)开始做总结,复习一下已经刷过的题目.到目前为止leetcode刷题323道. 2020/8/24 完成题目整 ...

  8. LeetCode All in One 题目讲解汇总(持续更新中...)

    原文地址:https://www.cnblogs.com/grandyang/p/4606334.html 终于将LeetCode的大部分题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开 ...

  9. C#LeetCode刷题-队列

    队列篇 # 题名 刷题 通过率 难度 363 矩形区域不超过 K 的最大数值和 27.2% 困难 621 任务调度器 40.9% 中等 622 设计循环队列 C#LeetCode刷题之#622-设计循 ...

最新文章

  1. Python设计模式面向对象编程
  2. 网络协议 19 - RPC 协议:远在天边近在眼前
  3. boost::units模块实现测试显式和隐式单位转换
  4. King Gym - 102471H
  5. Git如何处理代码冲突
  6. BootStrap中Affix控件的使用方法及如何保持布局的美观
  7. python3基础(九)内置函数
  8. java Context类
  9. sqoop-1.4.7安装
  10. 【Android 12 AOSP学习】Android 12源码下载编译
  11. IT行业都有哪些职位,初学者如何选择才能够快速进入这个行业?
  12. 操作系统中的基础抽象
  13. 应用概率统计(陈魁)第十一章(回归分析)部分课后答案
  14. 腾讯面试Android开发
  15. Firefox插件的安装及使用方法(持续更新中)
  16. Birds in Forest
  17. 不靠体育赛事,咪咕视频还有多大发展空间?
  18. Android之Manifest文件
  19. 《金税盘--发票开具、发票领购、发票安全存储、发票管理、身份认证和抄报税功能详解》
  20. vue耦合程度是什么意思

热门文章

  1. Python map/reduce
  2. laravel 队列学习
  3. LightOJ 1422 区间DP Halloween Costumes
  4. 每天学一点flash(14) as3.0 处理xml (官方)
  5. jQuery Validate 前端校验
  6. Struts2的通配符配置方式
  7. Android 微信登录
  8. js cookie操作
  9. String类得常用方法
  10. java 迭代器的原理_Java集合框架迭代器Iterator实现原理解析