-
Notifications
You must be signed in to change notification settings - Fork 3
/
solution-class.ts
55 lines (44 loc) · 1.04 KB
/
solution-class.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { CircularQueue } from './types';
/*
* @lc app=leetcode id=622 lang=javascript
*
* [622] Design Circular Queue
*/
class MyCircularQueue implements CircularQueue {
private head = 0;
private tail = -1;
private max: number;
private length = 0;
private queue: (number | null)[] = [];
constructor(k: number) {
this.max = k;
return this;
}
enQueue(value: number): boolean {
if (this.isFull()) return false;
this.tail = (this.tail + 1) % this.max;
this.queue[this.tail] = value;
this.length++;
return true;
}
deQueue(): boolean {
if (this.isEmpty()) return false;
this.queue[this.head] = null;
this.head = (this.head + 1) % this.max;
this.length--;
return true;
}
Front(): number {
return this.isEmpty() ? -1 : this.queue[this.head]!;
}
Rear(): number {
return this.isEmpty() ? -1 : this.queue[this.tail]!;
}
isEmpty(): boolean {
return this.length === 0;
}
isFull(): boolean {
return this.length === this.max;
}
}
export { MyCircularQueue };