-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
/
Copy pathSolution.ts
113 lines (97 loc) · 2.33 KB
/
Solution.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
class MyCircularDeque {
private vals: number[];
private length: number;
private size: number;
private start: number;
private end: number;
constructor(k: number) {
this.vals = new Array(k).fill(0);
this.start = 0;
this.end = 0;
this.length = 0;
this.size = k;
}
insertFront(value: number): boolean {
if (this.isFull()) {
return false;
}
if (this.start === 0) {
this.start = this.size - 1;
} else {
this.start--;
}
this.vals[this.start] = value;
this.length++;
return true;
}
insertLast(value: number): boolean {
if (this.isFull()) {
return false;
}
this.vals[this.end] = value;
this.length++;
if (this.end + 1 === this.size) {
this.end = 0;
} else {
this.end++;
}
return true;
}
deleteFront(): boolean {
if (this.isEmpty()) {
return false;
}
if (this.start + 1 === this.size) {
this.start = 0;
} else {
this.start++;
}
this.length--;
return true;
}
deleteLast(): boolean {
if (this.isEmpty()) {
return false;
}
if (this.end === 0) {
this.end = this.size - 1;
} else {
this.end--;
}
this.length--;
return true;
}
getFront(): number {
if (this.isEmpty()) {
return -1;
}
return this.vals[this.start];
}
getRear(): number {
if (this.isEmpty()) {
return -1;
}
if (this.end === 0) {
return this.vals[this.size - 1];
}
return this.vals[this.end - 1];
}
isEmpty(): boolean {
return this.length === 0;
}
isFull(): boolean {
return this.length === this.size;
}
}
/**
* Your MyCircularDeque object will be instantiated and called as such:
* var obj = new MyCircularDeque(k)
* var param_1 = obj.insertFront(value)
* var param_2 = obj.insertLast(value)
* var param_3 = obj.deleteFront()
* var param_4 = obj.deleteLast()
* var param_5 = obj.getFront()
* var param_6 = obj.getRear()
* var param_7 = obj.isEmpty()
* var param_8 = obj.isFull()
*/