当前位置:网站首页>LeetCode——622.设计循环队列
LeetCode——622.设计循环队列
2022-08-03 11:03:00 【Cap07】
package j12;
import java.util.*;
class A {
int x[] = new int[1000];
void addx(int location, int value) {
x[location] = value;
}
}
class MyCircularQueue {
A a = new A();
int length = 0;
int user = 0;
int rear_location = 0;
int front_location = 0;
public MyCircularQueue(int k) {
length = k;
}
public boolean enQueue(int value) {
if (user < length) {
a.x[rear_location] = value;
// a.addx(rear_location,value);
rear_location++;
user++;
return true;
}
else {
return false;
}
}
public boolean deQueue() {
if (length == 0||rear_location==front_location) {
return false;
} else {
user--;
front_location++;
return true;
}
}
public int Front() {
if (length == 0||rear_location==front_location) {
return -1;
} else {
return a.x[front_location];
}
}
public int Rear() {
if (length == 0||rear_location==front_location) {
return -1;
} else {
int temp=rear_location;
temp--;
return a.x[temp];
}
}
public boolean isEmpty() {
if (user == 0) {
return true;
} else {
return false;
}
}
public boolean isFull() {
if(user==length){
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();
*/
public class j13 {
public static void main(String args[]) {
MyCircularQueue circularQueue = new MyCircularQueue(6);
System.out.println(circularQueue.enQueue(6));
System.out.println(circularQueue.Rear());
System.out.println(circularQueue.Rear());
System.out.println(circularQueue.deQueue());
System.out.println(circularQueue.enQueue(5));
System.out.println(circularQueue.Rear());
System.out.println(circularQueue.deQueue());
System.out.println(circularQueue.Front());
System.out.println(circularQueue.deQueue());
System.out.println(circularQueue.deQueue());
System.out.println(circularQueue.deQueue());
// MyCircularQueue circularQueue = new MyCircularQueue(3); // 设置长度为 3
// System.out.println(circularQueue.enQueue(1)); // 返回 true
// System.out.println(circularQueue.enQueue(2)); // 返回 true
// System.out.println(circularQueue.enQueue(3)); // 返回 true
// System.out.println(circularQueue.enQueue(4)); // 返回 false,队列已满
// System.out.println(circularQueue.Rear()); // 返回 3
// System.out.println(circularQueue.isFull()); // 返回 true
// System.out.println(circularQueue.deQueue()); // 返回 true
// System.out.println(circularQueue.enQueue(4)); // 返回 true
// System.out.println(circularQueue.Rear()); // 返回 4
}
}
边栏推荐
- Dry goods!A highly structured and sparse linear transformation called Deformable Butterfly (DeBut)
- 面试官:工作两年了,这么简单的算法题你都不会?
- 干货!一种被称为Deformable Butterfly(DeBut)的高度结构化且稀疏的线性变换
- fast planner中拓扑路径搜索
- 【二分查找详解外加递归写法】附有全部代码
- 成为优秀架构师必备技能:怎样才能画出让所有人赞不绝口的系统架构图?秘诀是什么?快来打开这篇文章看看吧!...
- 通过组策略安装软件和删除用户配置文件
- 怎么在外头使用容器里php命令
- Depth study of 100 cases - convolution neural network (CNN) to realize the clothing image classification
- What is the ERC20 token standard?
猜你喜欢
随机推荐
Simple implementation of a high-performance clone of Redis using .NET (1)
for in 和 for of的区别
Summary of redis basics - data types (strings, lists, sets, hashes, sets)
如何改变sys_guid() 返回值类型
"Global Digital Economy Conference" landed in N World, Rongyun provides communication cloud service support
LP流动性挖矿DAPP系统开发丨流动性挖矿功能原理及说明
What is the relationship between The Matrix and 6G?
跨链桥协议 Nomad 遭遇黑客攻击,损失超 1.5 亿美元
MapReduce中ETL数据清洗案例
关于OPENSSL的问题
SAP 电商云 Spartacus UI 的 External Routes 设计明细
Cross-chain bridge protocol Nomad suffers hacker attack, losing more than $150 million
Binary search tree (search binary tree) simulation implementation (there is a recursive version)
基于PHP7.2+MySQL5.7的回收租凭系统
【TypeScript】为什么要选择 TypeScript?
[Star Project] Little Hat Plane Battle (9)
全新的Uber App设计
complete knapsack problem
试题G:单词分析 ← 第十一届蓝桥杯大赛第二场省赛赛题
OS层面包重组失败过高,数据库层面gc lost 频繁









