问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4126 访问。

设计你的循环队列实现。 循环队列是一种线性数据结构,其操作表现基于 FIFO(先进先出)原则并且队尾被连接在队首之后以形成一个循环。它也被称为“环形缓冲器”。
循环队列的一个好处是我们可以利用这个队列之前用过的空间。在一个普通队列里,一旦一个队列满了,我们就不能插入下一个元素,即使在队列前面仍有空间。但是使用循环队列,我们能使用这些空间去存储新的值。
你的实现应该支持如下操作:

MyCircularQueue(k): 构造器,设置队列长度为 k 。
Front: 从队首获取元素。如果队列为空,返回 -1 。
Rear: 获取队尾元素。如果队列为空,返回 -1 。
enQueue(value): 向循环队列插入一个元素。如果成功插入则返回真。
deQueue(): 从循环队列中删除一个元素。如果成功删除则返回真。
isEmpty(): 检查循环队列是否为空。
isFull(): 检查循环队列是否已满。

MyCircularQueue circularQueue = new MyCircularQueue(3); // 设置长度为3

circularQueue.enQueue(1);  // 返回true

circularQueue.enQueue(2);  // 返回true

circularQueue.enQueue(3);  // 返回true

circularQueue.enQueue(4);  // 返回false,队列已满

circularQueue.Rear();  // 返回3

circularQueue.isFull();  // 返回true

circularQueue.deQueue();  // 返回true

circularQueue.enQueue(4);  // 返回true

circularQueue.Rear();  // 返回4

提示:

  • 所有的值都在 1 至 1000 的范围内;
  • 操作数将在 1 至 1000 的范围内;
  • 请不要使用内置的队列库。

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 can not 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.

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 4

Note:

  • All values will be in the range of [1, 1000].
  • The number of operations will be in the range of [1, 1000].
  • Please do not use the built-in Queue library.

示例

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4126 访问。

public class Program {public static void Main(string[] args) {var circularQueue = new MyCircularQueue(3);Console.WriteLine(circularQueue.EnQueue(1));Console.WriteLine(circularQueue.EnQueue(2));Console.WriteLine(circularQueue.EnQueue(3));Console.WriteLine(circularQueue.EnQueue(4));Console.WriteLine(circularQueue.Rear());Console.WriteLine(circularQueue.IsFull());Console.WriteLine(circularQueue.DeQueue());Console.WriteLine(circularQueue.EnQueue(4));Console.WriteLine(circularQueue.Rear());Console.ReadKey();}public class MyCircularQueue {private int _index = -1;private int _count = 0;private List<int> _list = null;public MyCircularQueue(int k) {//构造器,设置队列长度为 k_count = k;_list = new List<int>();}public bool EnQueue(int value) {//向循环队列插入一个元素。如果成功插入则返回真if(_count == 0 || _index == _count - 1) return false;_index++;_list.Add(value);return true;}public bool DeQueue() {//从循环队列中删除一个元素。如果成功删除则返回真if(_count == 0 || _index == -1) return false;_index--;_list.RemoveAt(0);return true;}public int Front() {//从队首获取元素。如果队列为空,返回 -1if(_count == 0 || _index == -1) return -1;return _list[0];}public int Rear() {//获取队尾元素。如果队列为空,返回 -1if(_count == 0 || _index == -1) return -1;return _list[_index];}public bool IsEmpty() {//检查循环队列是否为空return _index == -1;}public bool IsFull() {//检查循环队列是否已满return _index == _count - 1;}}}

以上给出1种算法实现,以下是这个案例的输出结果:

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4126 访问。

True
True
True
False
3
True
True
True
4

分析:

显而易见,以上算法中所有的方法(MyCircularQueue、EnQueue、DeQueue、Front Rear、IsEmpty、IsFull)的时间复杂度均为:  。

C#LeetCode刷题之#622-设计循环队列​​​​​​​(Design Circular Queue)相关推荐

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

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

  2. C#LeetCode刷题之#232-用栈实现队列​​​​​​​​​​​​​​(Implement Queue using Stacks)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4108 访问. 使用栈实现队列的下列操作: push(x) -- ...

  3. c++数据结构中 顺序队列的队首队尾_yiduobo的每日leetcode 622.设计循环队列

    祖传的手艺不想丢了,所以按顺序写一个leetcode的题解.计划每日两题,争取不卡题吧. 622.设计循环队列https://leetcode-cn.com/problems/design-circu ...

  4. Leetcode 622. 设计循环队列

    Leetcode 622. 设计循环队列学习 分析 代码 参考链接 分析 循环队列,自己做题时没考虑怎么实现,可以通过索引下标除以数组的长度实现循环.循环队列的队首元素和队尾元素是动态变化的,删除一个 ...

  5. leetcode 622——设计循环队列

    leetcode 622--设计循环队列(C语言提交) 题目链接:leetcode 622--设计循环队列 题目描述: 设计你的循环队列实现. 循环队列是一种线性数据结构,其操作表现基于 FIFO(先 ...

  6. Java实现 LeetCode 622 设计循环队列(暴力大法)

    622. 设计循环队列 设计你的循环队列实现. 循环队列是一种线性数据结构,其操作表现基于 FIFO(先进先出)原则并且队尾被连接在队首之后以形成一个循环.它也被称为"环形缓冲器" ...

  7. 622. 设计循环队列(C实现)

    题目 题目链接:622. 设计循环队列 思路 设计循环队列有两种实现方式 第一种是使用数组实现: 第二种是使用单链表实现: 而设计循环队列不管是使用数组实现还是使用单链表实现,都需要给数组预留出一个空 ...

  8. 循环队列(Circular Queue)

    循环队列作用 为充分利用向量空间,克服"假溢出"现象的方法. 循环队列的原理 在环状顺序表中,最后一个位置(a[6])和第一个位置(a[0])紧挨着,这样做的好处是: 随着元素做入 ...

  9. 【Java】 LeetCode 622. 设计循环队列 (有关实现循环队列的讲解)

    题目: 设计你的循环队列实现. 循环队列是一种线性数据结构,其操作表现基于 FIFO(先进先出)原则并且队尾被连接在队首之后以形成一个循环.它也被称为"环形缓冲器". 循环队列的一 ...

最新文章

  1. C++容器的选择和详细操作方法总结(有自己总结)
  2. go get github.com/astaxie/beego 没有反应
  3. [XSY3382] 专家系统(二分+线段树)
  4. 机器学习笔记六之神经网络的学习
  5. s1机试补考补习 9206
  6. Mybatis(2)——Mapper映射文件
  7. #高等数学# 第八章 微分方程
  8. POJ 1129 Channel Allocation(四色定理)
  9. 通过定义函数,来实现判断1-100之间奇数的目的
  10. 计算机uc,UC浏览器
  11. 1064 朋友数 (C++)
  12. 金手指(通达信公式 主图 源码 测试图)箱底 箱顶 短趋势线 中趋势线 压力位 支撑位
  13. Java 基础入门,小白提升路线图
  14. LeetCode 810 Chalkboard XOR Game【思维】
  15. 第一章:Google简介
  16. https://blog.csdn.net/codezjx/article/details/8872090
  17. 内网搭建maven私库
  18. 随便记录一下:微信公众号后台管理系统,获取需要扫描的二维码
  19. 软考高级之系统架构设计师系列【1】软考介绍
  20. 支付接口如何申请?商户申请条件是什么?

热门文章

  1. 空洞卷积(dilated convolution)
  2. 匿名内部类编译时生成多个class文件
  3. LeetCode 581. Shortest Unsorted Continuous Subarray
  4. Linux—编写shell脚本操作数据库执行sql
  5. Ubuntu 16.04 UUID 开机自动挂载硬盘
  6. 获取线程名称 java 1615387415
  7. 前端开发 常用选择符与权重0229
  8. jdk安装与调试笔记 20200202
  9. linux crontab定时任务详解
  10. 微信小程序开发之路(二)