当前位置:网站首页>LeetCode - 641 设计循环双端队列(设计)*
LeetCode - 641 设计循环双端队列(设计)*
2022-07-25 15:32:00 【三岁就很萌@D】



class MyCircularDeque {
//底层数组结构
private int[] deque;
//头指针
private int head;
//尾指针
private int tail;
//队列中实际中存放的数的个数
private int size;
//队列容量
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(isFull() == true)
return false;
else{
//在头指针指向位置插入value
deque[head] = value;
//头指针向前移动
head = (head -1 + this.capacity) % this.capacity;
//队列值个数加一
size += 1;
return true;
}
}
public boolean insertLast(int value) {
//如果队列已经满了
if(isFull() == true)
return false;
else{
//尾指针向后移动
tail = (tail + 1 + this.capacity) % this.capacity;
//在尾指针指向位置插入value
deque[tail] = value;
//队列值个数加一
size += 1;
return true;
}
}
public boolean deleteFront() {
//如果队列为空
if(isEmpty())
return false;
else{
head = (head + 1) % this.capacity;
size -= 1;
return true;
}
}
public boolean deleteLast() {
//如果队列为空
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(); */
边栏推荐
- p4552-差分
- Cf888g clever dictionary tree + violent divide and conquer (XOR minimum spanning tree)
- Node learning
- 2021上海市赛-D-卡特兰数变种,dp
- C#精挑整理知识要点11 委托和事件(建议收藏)
- 2016CCPC网络选拔赛C-换根dp好题
- CF888G-巧妙字典树+暴力分治(异或最小生成树)
- Pat class a topic directory
- ML - 图像 - 深度学习和卷积神经网络
- C # fine sorting knowledge points 10 generic (recommended Collection)
猜你喜欢

LeetCode - 380 O(1) 时间插入、删除和获取随机元素 (设计 哈希表+数组)

How to solve the login problem after the 30 day experience period of visual stuido2019

Take you to create your first C program (recommended Collection)

Pytorch学习笔记--常用函数总结3

P4552 differential

Cf888g clever dictionary tree + violent divide and conquer (XOR minimum spanning tree)

谷歌云盘如何关联Google Colab

p4552-差分

matlab---错误使用 var 数据类型无效。第一个输入参数必须为单精度值或双精度值

window系统黑窗口redis报错20Creating Server TCP listening socket *:6379: listen: Unknown error19-07-28
随机推荐
理解“平均负载”
Application of C language array in Sanzi chess -- prototype of Queen n problem
ML - 自然语言处理 - 关键技术
ML - 语音 - 传统语音模型
Games101 review: Transformation
Qtime定义(手工废物利用简单好看)
SQL cultivation manual from scratch - practical part
Pytorch学习笔记--常用函数总结2
Xcode added mobileprovision certificate file error: Xcode encoded an error
《图书馆管理系统——“借书还书”模块》项目研发阶段性总结
PageHelper does not take effect, and SQL does not automatically add limit
Take you to learn more about JS basic grammar (recommended Collection)
数据系统分区设计 - 分区再平衡(rebalancing)
2016 CCPC network trial c-change root DP good question
Pytorch框架练习(基于Kaggle Titanic竞赛)
2021 Shanghai match-h-two point answer
Get the ask code corresponding to the key pressed by the keyboard
UITextField的inputView和inputAccessoryView注意点
C#精挑整理知识要点11 委托和事件(建议收藏)
伤透脑筋的CPU 上下文切换