问题

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

设计实现双端队列。

你的实现需要支持以下操作:

MyCircularDeque(k):构造函数,双端队列的大小为k。

insertFront():将一个元素添加到双端队列头部。 如果操作成功返回 true。

insertLast():将一个元素添加到双端队列尾部。如果操作成功返回 true。

deleteFront():从双端队列头部删除一个元素。 如果操作成功返回 true。

deleteLast():从双端队列尾部删除一个元素。如果操作成功返回 true。

getFront():从双端队列头部获得一个元素。如果双端队列为空,返回 -1。

getRear():获得双端队列的最后一个元素。 如果双端队列为空,返回 -1。

isEmpty():检查双端队列是否为空。

isFull():检查双端队列是否满了。

MyCircularDeque circularDeque = new MyCircularDeque(3);    // 设置容量大小为3

circularDeque.insertLast(1);               // 返回 true

circularDeque.insertLast(2);              // 返回 true

circularDeque.insertFront(3);            // 返回 true

circularDeque.insertFront(4);            // 已经满了,返回 false

circularDeque.getRear();                    // 返回  32 -此处应为2,LeetCode官方翻译错误,2018-11-08

circularDeque.isFull();                         // 返回 true

circularDeque.deleteLast();                // 返回 true

circularDeque.insertFront(4);            // 返回 true

circularDeque.getFront();                   // 返回 4

提示:

  • 所有值的范围为 [1, 1000]
  • 操作次数的范围为 [1, 1000]
  • 请不要使用内置的双端队列库。

Design your implementation of the circular double-ended queue (deque).

Your implementation should support following operations:

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.

MyCircularDeque circularDeque = new MycircularDeque(3);    // set the size to be 3

circularDeque.insertLast(1);               // return true

circularDeque.insertLast(2);              // return true

circularDeque.insertFront(3);            // return true

circularDeque.insertFront(4);            // return false, the queue is full

circularDeque.getRear();                    // return 32 -此处应为2,LeetCode官方翻译错误,2018-11-08

circularDeque.isFull();                        // return true

circularDeque.deleteLast();               // return true

circularDeque.insertFront(4);            // return true

circularDeque.getFront();                  // 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 Deque library.

示例

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

public class Program {

    public static void Main(string[] args) {
var circularDeque = new MyCircularDeque(5); Console.WriteLine(circularDeque.InsertFront(7));
Console.WriteLine(circularDeque.InsertLast(0));
Console.WriteLine(circularDeque.InsertLast(3));
Console.WriteLine(circularDeque.InsertFront(9)); Console.WriteLine(circularDeque.DeleteLast());
Console.WriteLine(circularDeque.GetRear()); Console.WriteLine(); var circularDeque2 = new MyCircularDeque2(5); Console.WriteLine(circularDeque2.InsertFront(1));
Console.WriteLine(circularDeque2.InsertLast(2)); Console.WriteLine(circularDeque2.InsertLast(3)); Console.WriteLine(circularDeque2.InsertFront(4)); Console.WriteLine(circularDeque2.GetRear());
Console.WriteLine(circularDeque2.IsFull());
Console.WriteLine(circularDeque2.DeleteLast());
Console.WriteLine(circularDeque2.GetRear()); Console.WriteLine(circularDeque2.InsertFront(4));
Console.WriteLine(circularDeque2.GetFront()); Console.ReadKey();
} public class MyCircularDeque { private int _frontIndex = -1;
private int _rearIndex = -1; private int _count = 0; private List<int> _frontList = null;
private List<int> _rearList = null; public MyCircularDeque(int k) {
//构造函数,双端队列的大小为k
_count = k;
_frontList = new List<int>();
_rearList = new List<int>();
} public bool InsertFront(int value) {
//将一个元素添加到双端队列头部。 如果操作成功返回 true
if(_count == 0 || _frontIndex + _rearIndex == _count - 2) return false;
_frontIndex++;
_frontList.Add(value);
return true;
} public bool InsertLast(int value) {
//将一个元素添加到双端队列尾部。如果操作成功返回 true
if(_count == 0 || _frontIndex + _rearIndex == _count - 2) return false;
_rearIndex++;
_rearList.Insert(0, value);
return true;
} public bool DeleteFront() {
//从双端队列头部删除一个元素。 如果操作成功返回 true
if(_count == 0 || _frontIndex + _rearIndex == -2) return false;
if(_frontIndex == -1) {
_rearList.RemoveAt(_rearIndex);
_rearIndex--;
return true;
}
_frontList.RemoveAt(_frontIndex);
_frontIndex--;
return true;
} public bool DeleteLast() {
//从双端队列尾部删除一个元素。如果操作成功返回 true
if(_count == 0 || _frontIndex + _rearIndex == -2) return false;
if(_rearIndex == -1) {
_frontIndex--;
_frontList.RemoveAt(0);
return true;
}
_rearIndex--;
_rearList.RemoveAt(0);
return true;
} public int GetFront() {
//从双端队列头部获得一个元素。如果双端队列为空,返回 -1
if(_count == 0 || _frontIndex + _rearIndex == -2) return -1;
if(_frontIndex == -1) {
return _rearList[_rearIndex];
}
return _frontList[_frontIndex];
} public int GetRear() {
//获得双端队列的最后一个元素。 如果双端队列为空,返回 -1
if(_count == 0 || _frontIndex + _rearIndex == -2) return -1;
if(_rearIndex == -1) {
return _frontList[0];
}
return _rearList[0];
} public bool IsEmpty() {
//检查双端队列是否为空
return _frontIndex + _rearIndex == -2;
} public bool IsFull() {
//检查双端队列是否满了
return _frontIndex + _rearIndex == _count - 2;
} } public class MyCircularDeque2 { private List<int> _list = null;
private int _count = 0; public MyCircularDeque2(int k) {
_list = new List<int>();
_count = k;
} public bool InsertFront(int value) {
if(_count > _list.Count) {
_list.Insert(0, value);
return true;
}
return false;
} public bool InsertLast(int value) {
if(_count > _list.Count) {
_list.Add(value);
return true;
}
return false;
} public bool DeleteFront() {
if(_list.Count > 0) {
_list.RemoveAt(0);
return true;
}
return false;
} public bool DeleteLast() {
if(_list.Count > 0) {
_list.RemoveAt(_list.Count - 1);
return true;
}
return false;
} public int GetFront() {
return _list.Count > 0 ? _list[0] : -1;
} public int GetRear() {
return _list.Count > 0 ? _list[_list.Count - 1] : -1;
} public bool IsEmpty() {
return _list.Count <= 0;
} public bool IsFull() {
return _list.Count == _count;
} } }

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

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

True
True
True
True
True
0 True
True
True
True
3
False
True
2
True
4

分析:

显而易见,以上2种算法中所有的方法的时间复杂度均为:  。

C#LeetCode刷题之#641-设计循环双端队列(Design Circular Deque)的更多相关文章

  1. [Swift]LeetCode641. 设计循环双端队列 | Design Circular Deque

    Design your implementation of the circular double-ended queue (deque). Your implementation should su ...

  2. Java实现 LeetCode 641 设计循环双端队列(暴力)

    641. 设计循环双端队列 设计实现双端队列. 你的实现需要支持以下操作: MyCircularDeque(k):构造函数,双端队列的大小为k. insertFront():将一个元素添加到双端队列头 ...

  3. Leetcode641.Design Circular Deque设计循环双端队列

    设计实现双端队列. 你的实现需要支持以下操作: MyCircularDeque(k):构造函数,双端队列的大小为k. insertFront():将一个元素添加到双端队列头部. 如果操作成功返回 tr ...

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

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

  5. C#LeetCode刷题之#706-设计哈希映射(Design HashMap)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4116 访问. 不使用任何内建的哈希表库设计一个哈希映射 具体地说 ...

  6. C#LeetCode刷题之#705-设计哈希集合​​​​​​​(Design HashSet)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4114 访问. 不使用任何内建的哈希表库设计一个哈希集合 具体地说 ...

  7. java数据结构-11循环双端队列

    @SuppressWarnings("unchecked") public class CircleDeque<E> { private int front; priv ...

  8. C#LeetCode刷题-设计

    设计篇 # 题名 刷题 通过率 难度 146 LRU缓存机制   33.1% 困难 155 最小栈 C#LeetCode刷题之#155-最小栈(Min Stack) 44.9% 简单 173 二叉搜索 ...

  9. C#LeetCode刷题-队列

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

随机推荐

  1. corosync+pacemaker高可用集群

    高可用集群,是指以减少服务中断(如因服务器宕机等引起的服务中断)时间为目的的服务器集群技术.简单的说,集群就是一组计算机,它们作为一个整体向用户提供一组网络资源.这些单个的计算机系统就是集群的节点. ...

  2. Win7安装Python失败 提示Setup failed

    一.安装报错 如图所示,双击Python安装包后进行安装显示Setup failed 安装失败: 二.错误排除 1.首先查看自己的计算机是否已经安装了 Win7 Service Pack 1大补丁,没 ...

  3. UUID字符串使用

    UUID字符串使用 1.可以生成唯一的字符串标示,在发送请求中可能会用到 function uuid(num) { var s = []; var hexDigits = "01234567 ...

  4. ElasticSearch(四)查询、分词器

    正向索引 正排表是以文档的ID为关键字,表中记录文档中每个字的位置信息,查找时扫描表中每个文档中字的信息直到找出所有包含查询关键字的文档. 这种组织方法在建立索引的时候结构比较简单,建立比较方便且易于 ...

  5. Python编程入门(第3版)|百度网盘免费下载|零基础入门学习资料

    百度网盘免费下载:Python编程入门(第3版) 提取码:rsd7 目录  · · · · · · 第1章 编程简介 11.1 Python语言 21.2 Python适合用于做什么 31.3 程序员 ...

  6. Markdown显示测试

    这是一个一级标题 文本1 文本2 这是一个二级标题 斜体 粗体 粗斜体 下面是分割线 上面是分割线 删除线 下划线 脚注[1] 这是一个三级标题 无序列表1 内容 无序列表2 内容 无序列表3 有序列 ...

  7. OnePill本地保存用户的结构

    sharedPreferences存储的数据 int Code code 表示当前用户的类别:医生为1,用户为2. Sting user user 存储当前user的json字符串 String do ...

  8. php实现导出数据分类合并单元格功能

    <?php $conn = mysql_connect("localhost","root","root"); $db = mysql ...

  9. 《Python编程快速上手 —让繁琐工作自动化》|百度网盘免费下载|Python自动化办公

    Python编程快速上手—让繁琐工作自动化 提取码:u8vj 如今,人们面临的大多数任务都可以通过编写计算机软件来完成. Python 是一种解释型.面向对象.动态数据类型的高级程序设计语言.通过 P ...

  10. 社交网站的数据挖掘与分析pdf版本|网盘下载地址附提取码|

    点击此处进入网盘下载地址 提取码:btqx 作者介绍: 马修·罗塞尔(MatthewA.Russell),DigitalReasoningSystems公司的技术副总裁和Zaffra公司的负责人,是热 ...