问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 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 访问。

  1. public class Program {
  2. public static void Main(string[] args) {
  3. var circularQueue = new MyCircularQueue(3);
  4. Console.WriteLine(circularQueue.EnQueue(1));
  5. Console.WriteLine(circularQueue.EnQueue(2));
  6. Console.WriteLine(circularQueue.EnQueue(3));
  7. Console.WriteLine(circularQueue.EnQueue(4));
  8. Console.WriteLine(circularQueue.Rear());
  9. Console.WriteLine(circularQueue.IsFull());
  10. Console.WriteLine(circularQueue.DeQueue());
  11. Console.WriteLine(circularQueue.EnQueue(4));
  12. Console.WriteLine(circularQueue.Rear());
  13. Console.ReadKey();
  14. }
  15. public class MyCircularQueue {
  16. private int _index = -1;
  17. private int _count = 0;
  18. private List<int> _list = null;
  19. public MyCircularQueue(int k) {
  20. //构造器,设置队列长度为 k
  21. _count = k;
  22. _list = new List<int>();
  23. }
  24. public bool EnQueue(int value) {
  25. //向循环队列插入一个元素。如果成功插入则返回真
  26. if(_count == 0 || _index == _count - 1) return false;
  27. _index++;
  28. _list.Add(value);
  29. return true;
  30. }
  31. public bool DeQueue() {
  32. //从循环队列中删除一个元素。如果成功删除则返回真
  33. if(_count == 0 || _index == -1) return false;
  34. _index--;
  35. _list.RemoveAt(0);
  36. return true;
  37. }
  38. public int Front() {
  39. //从队首获取元素。如果队列为空,返回 -1
  40. if(_count == 0 || _index == -1) return -1;
  41. return _list[0];
  42. }
  43. public int Rear() {
  44. //获取队尾元素。如果队列为空,返回 -1
  45. if(_count == 0 || _index == -1) return -1;
  46. return _list[_index];
  47. }
  48. public bool IsEmpty() {
  49. //检查循环队列是否为空
  50. return _index == -1;
  51. }
  52. public bool IsFull() {
  53. //检查循环队列是否已满
  54. return _index == _count - 1;
  55. }
  56. }
  57. }

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

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

  1. True
  2. True
  3. True
  4. False
  5. 3
  6. True
  7. True
  8. True
  9. 4

分析:

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

C#LeetCode刷题之#622-设计循环队列​​​​​​​(Design Circular Queue)的更多相关文章

  1. LeetCode 622:设计循环队列 Design Circular Queue

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

  2. [Swift]LeetCode622. 设计循环队列 | Design Circular Queue

    Design your implementation of the circular queue. The circular queue is a linear data structure in w ...

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

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

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

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

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

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

  6. 622.设计循环队列 javascript实现

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

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

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

  8. C#LeetCode刷题之#707-设计链表(Design Linked List)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4118 访问. 设计链表的实现.您可以选择使用单链表或双链表.单链 ...

  9. leetcode刷题-60第k个队列

    题目 给出集合 [1,2,3,…,n],其所有元素共有 n! 种排列. 按大小顺序列出所有排列情况,并一一标记,当 n = 3 时, 所有排列如下: "123""132& ...

随机推荐

  1. 转载一篇关于kafka零拷贝(zero-copy)通俗易懂的好文

    原文地址 https://www.cnblogs.com/yizhou35/p/12026263.html 零拷贝就是一种避免CPU 将数据从一块存储拷贝到另外一块存储的技术. DMA技术是Direc ...

  2. webpack源码-loader的原理

    版本 webpack :"version": "3.12.0", webpack配置中的loaders配置是如何传递的 webpack/lib/NormalMo ...

  3. Python 实现图像快速傅里叶变换和离散余弦变换

    图像的正交变换在数字图像的处理与分析中起着很重要的作用,被广泛应用于图像增强.去噪.压缩编码等众多领域.本文手工实现了二维离散傅里叶变换和二维离散余弦变换算法,并在多个图像样本上进行测试,以探究二者的 ...

  4. 谷歌浏览器又隐藏的HTTPS和WWW前缀

    谷歌工程师 Emily Schechter 曾在 Chromium 反馈页面中提到:Chrome 团队一直将简易性.可用性.安全性作为衡量 UI 的标准.为了让 URL 能更好地被用户理解.移除那些容 ...

  5. apache 添加多个站点

    虚拟主机 (Virtual Host) 是在同一台机器搭建属于不同域名或者基于不同 IP 的多个网站服务的技术.可以为运行在同一物理机器上的各个网站指配不同的 IP 和端口,也可让多个网站拥有不同的域 ...

  6. INS(Instagram)如何绑定谷歌二次验证码/谷歌身份验证/双重认证?

    1.打开Ins,找到双重验证界面   打开Ins,点击右上角“三”-“设置”-“安全”-“双重验证”-“选择安全验证方式”-“身份验证应用”-“立即开启”-“手动设置”-“复制密钥”-“输入验证码” ...

  7. 微信小程序 springboot nginx 做图片存储 上传 浏览

    微信小程序前端-springboot后端-nginx图片存储 前言 本人小白一名,这是第一次学习微信小程序,特此做个记录. 首先准备nginx做图片存储 选择一个地址存放图片 #我的地址 [root@ ...

  8. nginx 的return配置

    该指令一般用于对请求的客户端直接返回响应状态码.在该作用域内return后面的所有nginx配置都是无效的. 可以使用在server.location以及if配置中. 除了支持跟状态码,还可以跟字符串 ...

  9. onepill Android端

    使用的框架 第三方登录集成基于ThinkPHP5的第三方登录插件 QQ第三方登录集成QQ互联.qq第三方接入 SharedPreference实现记住账号密码功能参考.参考2

  10. Python time altzone()方法

    描述 Python time altzone() 函数返回格林威治西部的夏令时地区的偏移秒数.高佣联盟 www.cgewang.com 如果该地区在格林威治东部会返回负值(如西欧,包括英国).对夏令时 ...