Java实现 LeetCode 622 设计循环队列(暴力大法)
622. 设计循环队列
设计你的循环队列实现。 循环队列是一种线性数据结构,其操作表现基于 FIFO(先进先出)原则并且队尾被连接在队首之后以形成一个循环。它也被称为“环形缓冲器”。
循环队列的一个好处是我们可以利用这个队列之前用过的空间。在一个普通队列里,一旦一个队列满了,我们就不能插入下一个元素,即使在队列前面仍有空间。但是使用循环队列,我们能使用这些空间去存储新的值。
你的实现应该支持如下操作:
MyCircularQueue(k): 构造器,设置队列长度为 k 。
Front: 从队首获取元素。如果队列为空,返回 -1 。
Rear: 获取队尾元素。如果队列为空,返回 -1 。
enQueue(value): 向循环队列插入一个元素。如果成功插入则返回真。
deQueue(): 从循环队列中删除一个元素。如果成功删除则返回真。
isEmpty(): 检查循环队列是否为空。
isFull(): 检查循环队列是否已满。
示例:
MyCircularQueue circularQueue = new MycircularQueue(3); // 设置长度为 3
circularQueue.enQueue(1); // 返回 true
circularQueue.enQueue(2); // 返回 true
circularQueue.enQueue(3); // 返回 true
circularQueue.enQueue(4); // 返回 false,队列已满
circularQueue.Rear(); // 返回 3
circularQueue.isFull(); // 返回 true
circularQueue.deQueue(); // 返回 true
circularQueue.enQueue(4); // 返回 true
circularQueue.Rear(); // 返回 4
提示:
所有的值都在 0 至 1000 的范围内;
操作数将在 1 至 1000 的范围内;
请不要使用内置的队列库。
class MyCircularQueue {
private Integer []arr;
private int head;
private int tail;
/** Initialize your data structure here. Set the size of the queue to be k. */
public MyCircularQueue(int k) {
arr=new Integer[k];
head=0;
tail=0;
}
/** Insert an element into the circular queue. Return true if the operation is successful. */
public boolean enQueue(int value) {
if(isFull()) {
return false;
}else {
arr[tail]=value;
tail=(tail+1)%(arr.length);
return true;
}
}
/** Delete an element from the circular queue. Return true if the operation is successful. */
public boolean deQueue() {
if(isEmpty()) {
return false;
}else {
arr[head]=null;
head=(head+1)%(arr.length);
return true;
}
}
/** Get the front item from the queue. */
public int Front() {
if(isEmpty()) {
return -1;
}else {
return arr[head];
}
}
/** Get the last item from the queue. */
public int Rear() {
if(isEmpty()) {
return -1;
}else {
if(tail!=0)return arr[tail-1];
else return arr[arr.length-1];
}
}
/** Checks whether the circular queue is empty or not. */
public boolean isEmpty() {
if(head==tail&&arr[head]==null) {
return true;
}else {
return false;
}
}
/** Checks whether the circular queue is full or not. */
public boolean isFull() {
if(head==tail&&arr[head]!=null) {
return true;
}else {
return false;
}
}
}
/**
* Your MyCircularQueue object will be instantiated and called as such:
* MyCircularQueue obj = new MyCircularQueue(k);
* boolean param_1 = obj.enQueue(value);
* boolean param_2 = obj.deQueue();
* int param_3 = obj.Front();
* int param_4 = obj.Rear();
* boolean param_5 = obj.isEmpty();
* boolean param_6 = obj.isFull();
*/
Java实现 LeetCode 622 设计循环队列(暴力大法)的更多相关文章
- LeetCode 622——设计循环队列
1. 题目 设计你的循环队列实现. 循环队列是一种线性数据结构,其操作表现基于 FIFO(先进先出)原则并且队尾被连接在队首之后以形成一个循环.它也被称为"环形缓冲器". 循环队列 ...
- Java实现 LeetCode 641 设计循环双端队列(暴力)
641. 设计循环双端队列 设计实现双端队列. 你的实现需要支持以下操作: MyCircularDeque(k):构造函数,双端队列的大小为k. insertFront():将一个元素添加到双端队列头 ...
- 622.设计循环队列 javascript实现
设计你的循环队列实现. 循环队列是一种线性数据结构,其操作表现基于 FIFO(先进先出)原则并且队尾被连接在队首之后以形成一个循环.它也被称为“环形缓冲器”. 循环队列的一个好处是我们可以利用这个队列 ...
- Java实现 LeetCode 649 Dota2 参议院(暴力大法)
649. Dota2 参议院 Dota2 的世界里有两个阵营:Radiant(天辉)和 Dire(夜魇) Dota2 参议院由来自两派的参议员组成.现在参议院希望对一个 Dota2 游戏里的改变作出决 ...
- Java实现 LeetCode 621 任务调度器(暴力大法)
621. 任务调度器 给定一个用字符数组表示的 CPU 需要执行的任务列表.其中包含使用大写的 A - Z 字母表示的26 种不同种类的任务.任务可以以任意顺序执行,并且每个任务都可以在 1 个单位时 ...
- Java实现 LeetCode 542 01 矩阵(暴力大法,正反便利)
542. 01 矩阵 给定一个由 0 和 1 组成的矩阵,找出每个元素到最近的 0 的距离. 两个相邻元素间的距离为 1 . 示例 1: 输入: 0 0 0 0 1 0 0 0 0 输出: 0 0 0 ...
- LeetCode 622:设计循环队列 Design Circular Queue
LeetCode 622:设计循环队列 Design Circular Queue 首先来看看队列这种数据结构: 队列:先入先出的数据结构 在 FIFO 数据结构中,将首先处理添加到队列中的第一个元素 ...
- [Swift]LeetCode622. 设计循环队列 | Design Circular Queue
Design your implementation of the circular queue. The circular queue is a linear data structure in w ...
- Java实现一个简单的循环队列
在某些时候,我们不能被要求像数组一样可以使用索引随机访问,而是需要被限制顺序处理业务,今天介绍一种先进先出(FIFO)的线性数据结构:队列, 当然,还有后进先出(LIFO)的处理方式,即为栈(后续有时 ...
随机推荐
- 【Kafka】Kafka集群基础操作!新手上路必备~
目录 bin目录 Topic命令概览 创建Topic 查看Topic 描述Topic Producer生产数据 Consumer消费数据 增加topic分区数 增加配置 删除配置 删除Topic 所有 ...
- 【MySQL基础总结】常用函数库
常用函数库 数学函数 分类及含义 示例 字符串函数 分类及含义 示例 日期时间函数 分类及含义 示例 条件判断函数 分类及含义 示例 系统函数 分类及含义 加密函数 分类及定义 其他常用函数 分类及含 ...
- 设计模式之GOF23建造者模式
组件很多,装配顺序不定 本质: 1,分离了对象子组件的单独构造(Builder负责)和装配(Director负责),从而可以构造出复杂的对象,这个模式适用于某个对象的构建过程复杂的情况下使用 2,实现 ...
- 掌握这10种方法帮你快速在Linux上分析二进制文件
我们每天都使用二进制文件,但对二进制文件知之甚少.二进制是指您每天运行的可执行文件,从命令行工具到成熟的应用程序.Linux提供了丰富的工具集,可轻松进行二进制分析!无论您的工作角色是什么,如果您在L ...
- 瞬间教你学会使用java中list的retainAll方法
retainAll方法简介 当我们有两个list集合的时候,我们可以使用retainAll方法求得两个list集合的子集.retainAll是Collection接口中提供的一个方法,各个实现类有自己 ...
- MYsql 8 连接报错 MySQLNonTransientConnectionException: Could not create connection to database server.
本地安装mysql 是8 项目中数据驱动 也要求是 8 <dependency> <groupId>mysql</groupId> <artifactId&g ...
- vue登录路由验证(转)
转载自:https://blog.csdn.net/github_39088222/article/details/80749219 vue的项目的登录状态(如果用vuex状态管理,页面一刷新vuex ...
- Java Concurrent包初探
同步容器: Vector/Hashtable:jdk1.0就已经存在,jdk1.2改写实现List/Map接口.作为ArrayList/HashMap在并发场景中的替代类出现.注意:Hashtable ...
- Angular和Ionic的路由跳转
一.Angular和Ionic的路由跳转 Angular的路由跳转: constructor(private router:Router){ } .... this.router.navigat ...
- Kubeedge-mapper 实现
应用场景: 利用GitHub上的温度传感器的例子作为讲解,实现从云端获取设备终端状态及使用Java模拟设备数据.其实和官网给的视频一样,只需要将终端设备的数据转换为支持MQTT协议传输的数据,云端就可 ...