LeetCode 641. 设计循环双端队列C#
原题链接
中等
作者:
hpstory
,
2022-08-15 14:19:46
,
所有人可见
,
阅读 83
C# 代码
public class MyCircularDeque {
private int[] queue;
private int front = 0;
private int end = 0;
private int capacity;
public MyCircularDeque(int k) {
this.queue = new int[k + 1];
this.capacity = k + 1;
}
public bool InsertFront(int value) {
if (this.IsFull()) return false;
this.front = this.GetIndex(this.front - 1);
this.queue[this.front] = value;
return true;
}
public bool InsertLast(int value) {
if (this.IsFull()) return false;
this.queue[this.end] = value;
this.end = this.GetIndex(this.end + 1);
return true;
}
public bool DeleteFront() {
if (this.IsEmpty()) return false;
this.front = this.GetIndex(this.front + 1);
return true;
}
public bool DeleteLast() {
if (this.IsEmpty()) return false;
this.end = this.GetIndex(this.end - 1);
return true;
}
public int GetFront() {
if (this.IsEmpty()) return -1;
return this.queue[this.front];
}
public int GetRear() {
if (this.IsEmpty()) return -1;
return this.queue[this.GetIndex(this.end - 1)];
}
public bool IsEmpty() {
return this.front == this.end;
}
public bool IsFull() {
return this.GetIndex(this.front - 1) == this.end;
}
private int GetIndex(int index){
return (index + this.capacity) % this.capacity;
}
}