当前位置:网站首页>Leetcode - 641 design cycle double ended queue (Design)*
Leetcode - 641 design cycle double ended queue (Design)*
2022-07-25 15:40:00 【Cute at the age of three @d】



class MyCircularDeque {
// Underlying array structure
private int[] deque;
// The head pointer
private int head;
// Tail pointer
private int tail;
// The number of actually stored numbers in the queue
private int size;
// Queue capacity
private int capacity;
public MyCircularDeque(int k) {
deque = new int[k];
head = 0;
tail = 0;
size = 0;
capacity = k;
}
public boolean insertFront(int value) {
// If the queue is full
if(isFull() == true)
return false;
else{
// Insert at the position pointed by the head pointer value
deque[head] = value;
// The head pointer moves forward
head = (head -1 + this.capacity) % this.capacity;
// Number of queue values plus one
size += 1;
return true;
}
}
public boolean insertLast(int value) {
// If the queue is full
if(isFull() == true)
return false;
else{
// The tail pointer moves backward
tail = (tail + 1 + this.capacity) % this.capacity;
// Insert at the position pointed by the tail pointer value
deque[tail] = value;
// Number of queue values plus one
size += 1;
return true;
}
}
public boolean deleteFront() {
// If the queue is empty
if(isEmpty())
return false;
else{
head = (head + 1) % this.capacity;
size -= 1;
return true;
}
}
public boolean deleteLast() {
// If the queue is empty
if(isEmpty())
return false;
else{
tail = (tail -1 + this.capacity) % this.capacity;
size -= 1;
return true;
}
}
public int getFront() {
if(isEmpty())
return -1;
else{
int front = (head + 1) % this.capacity;
return deque[front];
}
}
public int getRear() {
if(isEmpty())
return -1;
else{
return deque[tail];
}
}
public boolean isEmpty() {
return this.size == 0;
}
public boolean isFull() {
return this.capacity == this.size;
}
}
/** * Your MyCircularDeque object will be instantiated and called as such: * MyCircularDeque obj = new MyCircularDeque(k); * boolean param_1 = obj.insertFront(value); * boolean param_2 = obj.insertLast(value); * boolean param_3 = obj.deleteFront(); * boolean param_4 = obj.deleteLast(); * int param_5 = obj.getFront(); * int param_6 = obj.getRear(); * boolean param_7 = obj.isEmpty(); * boolean param_8 = obj.isFull(); */
边栏推荐
- MATLAB 如何生产随机复序列
- 2021江苏省赛A. Array-线段树,维护值域,欧拉降幂
- PAT甲级题目目录
- Icpc2021 Kunming m-violence + chairman tree
- PageHelper does not take effect, and SQL does not automatically add limit
- 《图书馆管理系统——“借书还书”模块》项目研发阶段性总结
- 数据系统分区设计 - 分区与二级索引
- CF685B-求有根树每颗子树的重心
- Cf365-e - Mishka and divisors, number theory +dp
- 哪里有搭建flink cdc抽mysql数的demo?
猜你喜欢
随机推荐
为什么PrepareStatement性能更好更安全?
MySQL—常用SQL语句整理总结
Pytorch框架练习(基于Kaggle Titanic竞赛)
C # fine sorting knowledge points 9 Set 2 (recommended Collection)
PAT甲级题目目录
2019陕西省省赛J-位运算+贪心
Singleton mode 3-- singleton mode
Are you ready to break away from the "involution circle"?
小波变换--dwt2 与wavedec2
2021江苏省赛A. Array-线段树,维护值域,欧拉降幂
MySQL - Summary of common SQL statements
p4552-差分
2021HNCPC-E-差分,思维
P4552 differential
数据系统分区设计 - 请求路由
C#精挑整理知识要点11 委托和事件(建议收藏)
LeetCode - 359 日志速率限制器 (设计)
如何实现页面包含
JVM—类加载器和双亲委派模型
4PAM在高斯信道与瑞利信道下的基带仿真系统实验







