当前位置:网站首页>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(); */
边栏推荐
- 数据系统分区设计 - 分区再平衡(rebalancing)
- 自定义注解校验API参数电话号
- I want to ask whether the variable configuration function can only be used in SQL mode
- Qtime定义(手工废物利用简单好看)
- Matlab randInt, matlab randInt function usage "recommended collection"
- 死锁杂谈
- Box avoiding mouse
- The difference between VaR, let and Const
- Pat grade a 1153 decode registration card of PAT (25 points)
- Take you to create your first C program (recommended Collection)
猜你喜欢

MySQL - Summary of common SQL statements

LeetCode - 379 电话目录管理系统(设计)

LeetCode - 303 区域和检索 - 数组不可变 (设计 前缀和数组)

Idea - click the file code to automatically synchronize with the directory

谷歌云盘如何关联Google Colab

Understanding the difference between wait() and sleep()
SQL cultivation manual from scratch - practical part

matlab 优化工具 manopt 安装

matlab 如何保存所有运行后的数据

为什么PrepareStatement性能更好更安全?
随机推荐
matlab 优化工具 manopt 安装
如何解决跨域问题
Endnote 无法编辑range 解决
C # fine sorting knowledge points 12 exception handling (recommended Collection)
数据系统分区设计 - 分区与二级索引
理解“平均负载”
MATLAB读取显示图像时数据格式转换原因
Gary Marcus: 学习语言比你想象的更难
2021 Shanghai match-b-ranked DP
Cf888g clever dictionary tree + violent divide and conquer (XOR minimum spanning tree)
谷歌博客:采用多重游戏决策Transformer训练通用智能体
MySQL—用户和权限管控
PAT甲级1152 Google Recruitment (20 分)
Games101 review: linear algebra
Pytorch学习笔记-刘二老师RNN高级篇-代码注释及结果
Box avoiding mouse
谷歌云盘如何关联Google Colab
Find out what happened in the process of new
二进制补码
Window system black window redis error 20creating server TCP listening socket *: 6379: listen: unknown error19-07-28