问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 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() {
//从队首获取元素。如果队列为空,返回 -1
if(_count == 0 || _index == -1) return -1;
return _list[0];
} public int Rear() {
//获取队尾元素。如果队列为空,返回 -1
if(_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. 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. MySql-Binlog协议

    MySQL主备复制原理 MySQL master 将数据变更写入二进制日志( binary log, 其中记录叫做二进制日志事件binary log events,可以通过 show binlog e ...

  2. C#数据结构与算法系列(二十三):归并排序算法(MergeSort)

    1.介绍 归并排序(MergeSort)是利用归并的思想实现的排序方法,该算法采用经典的分治策略(分治法将问题分(divide)成一些小的问题然后递归求解, 而治(conquer)的阶段则将分的阶段得 ...

  3. 【Python学习笔记六】获取百度搜索结果以及百度返回“百度安全验证”问题解决

    1.获取百度搜索结果页面主要是修改百度搜索url中的参数实现,例如查询的关键字为wd: 举例:https://www.baidu.com/s?wd=python",这样就可以查询到‘pyth ...

  4. Linux好学吗?怎么自学Linux?6个值得你去学习Linux的理由!

    两个多月前,我对日常工作做出了一个非常重要的决定-我决定从Windows切换到Linux,将其作为个人和办公笔记本电脑的主要系统.除了开始时一些小的驱动程序问题之外,切换非常平稳,我不会考虑返回.如果 ...

  5. Spring RestTemplate 的介绍和使用-入门

    RestTemplate是什么? 传统情况下在java代码里访问restful服务,一般使用Apache的HttpClient.不过此种方法使用起来太过繁琐.spring提供了一种简单便捷的模板类来进 ...

  6. TestNg失败重跑—解决使用 dataProvider 参数化用例次数冲突问题

    问题背景 在使用 testng 执行 UI 自动化用例时,由于 UI自动化的不稳定性,我们在测试的时候,往往会加上失败重跑机制.在不使用 @DataProvider 提供用例参数化时,是不会有什么问题 ...

  7. 不是吧,阿sir,2020年程序员要不好过?

    自从网传程序员到了35岁之后必须要转行,现在又有人传言:“疫情之下,程序员今年要过苦日子了,降薪裁员是大趋势.” 不是,我就不明白了,你们怎么就看不得程序员好呢?天天巴望着程序员降薪.转行.裁员…   ...

  8. APP自动化 -- swipe(滑动屏幕)

  9. python Web项目上线之服务器环境配置

    1.下载安装Xftp 安装成功后,登录服务器用户密码,登录成功后 使用Xftp 将下载好的python解释器linux压缩包放置在服务器根目录下(这里用的是python3.7) 2. 解压压缩包,安装 ...

  10. 两数相加(B站看视频总结)

    ''' 两数相加: 给出两个 非空 的链表用来表示两个非负的整数 各自的位数是按照逆序的方式存储的 每一个节点只能保存 一位数 示例: 输入:(2->4->3) + (5->6-&g ...