当前位置:网站首页>如何设计循环队列?快进来学习~
如何设计循环队列?快进来学习~
2022-08-01 23:58:00 【陈亦康】

分析:
两种解题思路,一种是计数法,还有一种是浪费空间法,为什么这么说呢,看下图:



解法一:
class MyCircularQueue {
public int[] array;
private int front;
private int rear;
private int usedSize;
public MyCircularQueue(int k) {
array = new int[k];
}
//插入数据
public boolean enQueue(int value) {
if(isFull()){
return false;
}
else{
array[rear] = value;
//不可以单纯的rear++;会溢出,就不是循环了
rear = (rear + 1) % array.length;
usedSize++;
return true;
}
}
//删除元素
public boolean deQueue() {
if(isEmpty()){
return false;
}
front = (front + 1) % array.length;
usedSize--;
return true;
}
//获取首元素
public int Front() {
if(isEmpty()){
return -1;
}
return array[front];
}
//获取尾元素
public int Rear() {
if(isEmpty()){
return -1;
}
return rear == 0 ? array[array.length - 1] : array[rear - 1];
}
//检查是否为空
public boolean isEmpty() {
if(usedSize == 0){
return true;
}
return false;
}
//是否为满
public boolean isFull() {
return usedSize == array.length;
}
}解法二:
class MyCircularQueue {
public int[] array;
private int front;
private int rear;
public MyCircularQueue(int k) {
array = new int[k + 1];
}
//插入数据
public boolean enQueue(int value) {
if(isFull()){
return false;
}
else{
array[rear] = value;
//不可以单纯的rear++;会溢出,就不是循环了
rear = (rear + 1) % array.length;
return true;
}
}
//删除元素
public boolean deQueue() {
if(isEmpty()){
return false;
}
front = (front + 1) % array.length;
return true;
}
//获取首元素
public int Front() {
if(isEmpty()){
return -1;
}
return array[front];
}
//获取尾元素
public int Rear() {
if(isEmpty()){
return -1;
}
return rear == 0 ? array[array.length - 1] : array[rear - 1];
}
//检查是否为空
public boolean isEmpty() {
if(rear == front){
return true;
}
return false;
}
//是否为满
public boolean isFull() {
return (rear + 1) % array.length == front;
}
}边栏推荐
- 【MySQL篇】初识数据库
- QML package management
- 根本上解决mysql启动失败问题Job for mysqld.service failed because the control process exited with error code
- Cash Ⅱ LeetCode_518_ change
- 为什么要使用MQ消息中间件?这几个问题必须拿下
- DOM 事件及事件委托
- Artifact XXXwar exploded Artifact is being deployed, please wait...(已解决)
- Sql之各种Join
- numpy.isclose
- Get piggy homestay (short-term rental) data
猜你喜欢

分享一份接口测试项目(非常值得练手)

经典文献阅读之--DLO

使用 Zadig 交付云原生微服务应用

OpenCV DNN blogFromImage() detailed explanation

Docker搭建Mysql主从复制

Flink Yarn Per Job - Yarn应用

Use Jenkins for continuous integration, this knowledge point must be mastered

WEB安全基础 - - - XRAY使用

【加密周报】经济衰退在加息气氛中蔓延 美联储“放手一搏”?盘点上周加密市场发生的重大事件

Work for 5 years, test case design is bad?To look at the big case design summary
随机推荐
20220725 Information update
【无标题】
OpenCV DNN blogFromImage()详解
多御安全浏览器android版更新至1.7,改进加密协议
技术分享 | 接口测试中如何使用Json 来进行数据交互 ?
【Leetcode】473. Matchsticks to Square
架构基本概念和架构本质
Quartus uses tcl files to quickly configure pins
在CDH的hue上的oozie出现,提交 Coordinator My Schedule 时出错
一篇永久摆脱Mysql时区错误问题,idea数据库可视化插件配置
企业防护墙管理,有什么防火墙管理工具?
很多人喜欢用多御安全浏览器,竟是因为这些原因
Flink学习第四天——完成第一个Flink 流批一体案例
cdh6 opens oozieWeb page, Oozie web console is disabled.
为什么要使用MQ消息中间件?这几个问题必须拿下
【MySQL篇】初识数据库
2022 6th Strong Net Cup Part WP
在MySQL登录时出现Access denied for user ‘root‘@‘localhost‘ (using password YES) 拒绝访问问题解决
yay 报错 response decoding failed: invalid character ‘<‘ looking for beginning of value;
ICLR 2022最佳论文:基于对比消歧的偏标签学习