当前位置:网站首页>365天挑战LeetCode1000题——Day 047 设计循环队列 循环队列
365天挑战LeetCode1000题——Day 047 设计循环队列 循环队列
2022-08-02 10:28:00 【ShowM3TheCode】
622. 设计循环队列
代码实现(首刷自解)
class MyCircularQueue {
private:
int* q;
int front, rear, size, maxSize;
public:
MyCircularQueue(int k) : q(new int[k]), front(0), rear(0), size(0),
maxSize(k) {
}
bool enQueue(int value) {
if (isFull()) return false;
q[rear] = value;
rear = (rear + 1) % maxSize;
size++;
return true;
}
bool deQueue() {
if (isEmpty()) return false;
front = (front + 1) % maxSize;
size--;
return true;
}
int Front() {
if (isEmpty()) return -1;
return q[front];
}
int Rear() {
if (isEmpty()) return -1;
return q[(maxSize + rear - 1) % maxSize];
}
bool isEmpty() {
return !size;
}
bool isFull() {
return size == maxSize;
}
};
/** * Your MyCircularQueue object will be instantiated and called as such: * MyCircularQueue* obj = new MyCircularQueue(k); * bool param_1 = obj->enQueue(value); * bool param_2 = obj->deQueue(); * int param_3 = obj->Front(); * int param_4 = obj->Rear(); * bool param_5 = obj->isEmpty(); * bool param_6 = obj->isFull(); */
边栏推荐
猜你喜欢
随机推荐
云原生应用平台的核心模块有哪些
R语言使用ggpubr包的ggtexttable函数可视化表格数据(直接绘制表格图或者在图像中添加表格数据)、设置theme主题参数自定义表格中表头内容的填充色(使用colnames.style参数)
Alibaba CTO Cheng Li: Alibaba Open Source History, Concept and Practice
logo 图标(php图片加文字水印)
LayaBox---TypeScript---Mixins
DVWA 通关记录 2 - 命令注入 Command Injection
LayaBox---TypeScript---Symbols
转转反爬攻防战
周鸿祎称微软抄袭 360 安全模式后发文否认;英特尔CEO基辛格回应市值被AMD超越:股价下跌是咎由自取|极客头条...
The R language uses the rollapply function in the zoo package to apply the specified function to the time series in a rolling manner and the window moves, and set the align parameter to specify that t
周杰伦新歌发布,爬取《Mojito》MV弹幕,看看粉丝们都说的些啥!
同样做软件测试,和月收入 3W 的学弟聊了一晚上,我彻底崩溃了
wireshark的安装教程(暖气片安装方法图解)
Oracle查询提示 ORA-00933 SQL command not properly ended 原因排查
21年毕业转行软件测试,从0收入到月薪过万,我真的很幸运...
R语言时间序列数据的平滑:使用KernSmooth包的dpill函数和locpoly函数对时间序列数据进行平滑以消除噪声
LayaBox---TypeScript---Namespaces and modules
基于列表的排队与叫号系统
R语言ggpubr包的ggbarplot函数可视化分组柱状图、设置add参数为mean_se可视化不同水平均值的柱状图并为柱状图添加误差线(se标准误差)、position参数自定义分组柱状图分离
DVWA Clearance Log 2 - Command Injection