C#LeetCode刷题之#641-设计循环双端队列(Design Circular Deque)
问题
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 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)的更多相关文章
- [Swift]LeetCode641. 设计循环双端队列 | Design Circular Deque
Design your implementation of the circular double-ended queue (deque). Your implementation should su ...
- Java实现 LeetCode 641 设计循环双端队列(暴力)
641. 设计循环双端队列 设计实现双端队列. 你的实现需要支持以下操作: MyCircularDeque(k):构造函数,双端队列的大小为k. insertFront():将一个元素添加到双端队列头 ...
- Leetcode641.Design Circular Deque设计循环双端队列
设计实现双端队列. 你的实现需要支持以下操作: MyCircularDeque(k):构造函数,双端队列的大小为k. insertFront():将一个元素添加到双端队列头部. 如果操作成功返回 tr ...
- C#LeetCode刷题之#622-设计循环队列(Design Circular Queue)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4126 访问. 设计你的循环队列实现. 循环队列是一种线性数据结构 ...
- C#LeetCode刷题之#706-设计哈希映射(Design HashMap)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4116 访问. 不使用任何内建的哈希表库设计一个哈希映射 具体地说 ...
- C#LeetCode刷题之#705-设计哈希集合(Design HashSet)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4114 访问. 不使用任何内建的哈希表库设计一个哈希集合 具体地说 ...
- java数据结构-11循环双端队列
@SuppressWarnings("unchecked") public class CircleDeque<E> { private int front; priv ...
- C#LeetCode刷题-设计
设计篇 # 题名 刷题 通过率 难度 146 LRU缓存机制 33.1% 困难 155 最小栈 C#LeetCode刷题之#155-最小栈(Min Stack) 44.9% 简单 173 二叉搜索 ...
- C#LeetCode刷题-队列
队列篇 # 题名 刷题 通过率 难度 363 矩形区域不超过 K 的最大数值和 27.2% 困难 621 任务调度器 40.9% 中等 622 设计循环队列 C#LeetCode刷题之#622 ...
随机推荐
- 没内鬼,来点干货!SQL优化和诊断
SQL优化与诊断 Explain诊断 Explain各参数的含义如下: 列名 说明 id 执行编号,标识select所属的行.如果在语句中没有子查询或关联查询,只有唯一的select,每行都将显示1. ...
- 服务注册与发现【Eureka】- Eureka简介
什么是服务治理 SpringCloud 封装了 Netflix 公司开发的 Eureka 模块来 实现服务治理. 在传统的rpc远程调用框架中,管理每个服务与服务之间依赖关系比较复杂,管理比较复杂,所 ...
- 题解 CF 1372 B
题目 传送门 题意 给出 \(n\),输出 \(a\) ,\(b\) (\(0 < a \leq b < n\)),使\(a+b=n\)且 \(\operatorname{lcm}(a,b ...
- CSS3伪类 :empty
:empty 种类:伪类选择器 版本:CSS3.0 用法:匹配每个没有子元素(包含文本)的元素. 例子: <!DOCTYPE html> <html> <head> ...
- vue : watch、computed、以及对象数组
watch和computed是vue框架中很重要的特性. 那么,他们是怎么作用于对象数组的? 今天我们就来探究一下. 上代码. <template> <div class=" ...
- 异常类throwable
一.Error 严重错误,系统内部的错误.无法通过处理,只能避免. 二.Exception 使用不当导致,是可以避免的. 异常分类: 1.编译时异常 编译时遇到的异常,若未处理,就会编译失败,必须进行 ...
- jsp课堂笔记4 javabean
Javabean是一个可重复使用的软件组件,实际上是一种java类 实现代码重复利用 易编写易维护易使用 jsp页面的主要任务是显示页面,不负责数据的逻辑业务处理 将数据处理过程中指派一个或多个bea ...
- 想理解JVM看了这篇文章,就知道了!(一)
前言 本章节属于Java进阶系列,前面关于设计模式讲解完了,有兴趣的童鞋可以翻看之前的博文,后面会讲解JVM的优化,整个系列会完整的讲解整个java体系与生态相关的中间件知识.本次将对jvm有更深 ...
- 打开chm文件时出现“无法显示此页:确保 Web 地址 //ieframe.dll/dnserrordiagoff.htm# 正确”的解决办法
当我们打开chm文件时遇到下面这种情况: 解决方法: 1.一般情况下无法显示网页:右键 chm文件属性里最下面有个"解除锁定",点击"解除锁定"按钮就可以了. ...
- python beautifulsoup基本用法-文档结构
一.BeautifulSoup概述 BeautifulSoup是python的一个库,用于接收一个HTML或XML字符串并对其进行格式化,然后使用提供的方法快速查找指定元素. 使用BeautifulS ...