Skip to content

Commit d8e861c

Browse files
committed
feat: add solutions to lc problem: No.0622
No.0622.Design Circular Queue
1 parent 1b012de commit d8e861c

File tree

8 files changed

+435
-33
lines changed

8 files changed

+435
-33
lines changed

solution/0600-0699/0622.Design Circular Queue/README.md

+134-4
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ circularQueue.Rear(); &nbsp;// 返回 4</pre>
6161

6262
```python
6363
class MyCircularQueue:
64-
6564
def __init__(self, k: int):
6665
self.q = [0] * k
6766
self.front = 0
@@ -84,9 +83,7 @@ class MyCircularQueue:
8483
return True
8584

8685
def Front(self) -> int:
87-
if self.isEmpty():
88-
return -1
89-
return self.q[self.front]
86+
return -1 if self.isEmpty() else self.q[self.front]
9087

9188
def Rear(self) -> int:
9289
if self.isEmpty():
@@ -182,6 +179,139 @@ class MyCircularQueue {
182179
*/
183180
```
184181

182+
### **C++**
183+
184+
```cpp
185+
class MyCircularQueue {
186+
private:
187+
int front;
188+
int size;
189+
int capacity;
190+
vector<int> q;
191+
192+
public:
193+
MyCircularQueue(int k) {
194+
capacity = k;
195+
q = vector<int>(k);
196+
front = size = 0;
197+
}
198+
199+
bool enQueue(int value) {
200+
if (isFull()) return false;
201+
int idx = (front + size) % capacity;
202+
q[idx] = value;
203+
++size;
204+
return true;
205+
}
206+
207+
bool deQueue() {
208+
if (isEmpty()) return false;
209+
front = (front + 1) % capacity;
210+
--size;
211+
return true;
212+
}
213+
214+
int Front() {
215+
if (isEmpty()) return -1;
216+
return q[front];
217+
}
218+
219+
int Rear() {
220+
if (isEmpty()) return -1;
221+
int idx = (front + size - 1) % capacity;
222+
return q[idx];
223+
}
224+
225+
bool isEmpty() {
226+
return size == 0;
227+
}
228+
229+
bool isFull() {
230+
return size == capacity;
231+
}
232+
};
233+
234+
/**
235+
* Your MyCircularQueue object will be instantiated and called as such:
236+
* MyCircularQueue* obj = new MyCircularQueue(k);
237+
* bool param_1 = obj->enQueue(value);
238+
* bool param_2 = obj->deQueue();
239+
* int param_3 = obj->Front();
240+
* int param_4 = obj->Rear();
241+
* bool param_5 = obj->isEmpty();
242+
* bool param_6 = obj->isFull();
243+
*/
244+
```
245+
246+
### **Go**
247+
248+
```go
249+
type MyCircularQueue struct {
250+
front int
251+
size int
252+
capacity int
253+
q []int
254+
}
255+
256+
func Constructor(k int) MyCircularQueue {
257+
q := make([]int, k)
258+
return MyCircularQueue{0, 0, k, q}
259+
}
260+
261+
func (this *MyCircularQueue) EnQueue(value int) bool {
262+
if this.IsFull() {
263+
return false
264+
}
265+
idx := (this.front + this.size) % this.capacity
266+
this.q[idx] = value
267+
this.size++
268+
return true
269+
}
270+
271+
func (this *MyCircularQueue) DeQueue() bool {
272+
if this.IsEmpty() {
273+
return false
274+
}
275+
this.front = (this.front + 1) % this.capacity
276+
this.size--
277+
return true
278+
}
279+
280+
func (this *MyCircularQueue) Front() int {
281+
if this.IsEmpty() {
282+
return -1
283+
}
284+
return this.q[this.front]
285+
}
286+
287+
func (this *MyCircularQueue) Rear() int {
288+
if this.IsEmpty() {
289+
return -1
290+
}
291+
idx := (this.front + this.size - 1) % this.capacity
292+
return this.q[idx]
293+
}
294+
295+
func (this *MyCircularQueue) IsEmpty() bool {
296+
return this.size == 0
297+
}
298+
299+
func (this *MyCircularQueue) IsFull() bool {
300+
return this.size == this.capacity
301+
}
302+
303+
/**
304+
* Your MyCircularQueue object will be instantiated and called as such:
305+
* obj := Constructor(k);
306+
* param_1 := obj.EnQueue(value);
307+
* param_2 := obj.DeQueue();
308+
* param_3 := obj.Front();
309+
* param_4 := obj.Rear();
310+
* param_5 := obj.IsEmpty();
311+
* param_6 := obj.IsFull();
312+
*/
313+
```
314+
185315
### **TypeScript**
186316

187317
```ts

solution/0600-0699/0622.Design Circular Queue/README_EN.md

+134-4
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ myCircularQueue.Rear(); // return 4
6262

6363
```python
6464
class MyCircularQueue:
65-
6665
def __init__(self, k: int):
6766
self.q = [0] * k
6867
self.front = 0
@@ -85,9 +84,7 @@ class MyCircularQueue:
8584
return True
8685

8786
def Front(self) -> int:
88-
if self.isEmpty():
89-
return -1
90-
return self.q[self.front]
87+
return -1 if self.isEmpty() else self.q[self.front]
9188

9289
def Rear(self) -> int:
9390
if self.isEmpty():
@@ -181,6 +178,139 @@ class MyCircularQueue {
181178
*/
182179
```
183180

181+
### **C++**
182+
183+
```cpp
184+
class MyCircularQueue {
185+
private:
186+
int front;
187+
int size;
188+
int capacity;
189+
vector<int> q;
190+
191+
public:
192+
MyCircularQueue(int k) {
193+
capacity = k;
194+
q = vector<int>(k);
195+
front = size = 0;
196+
}
197+
198+
bool enQueue(int value) {
199+
if (isFull()) return false;
200+
int idx = (front + size) % capacity;
201+
q[idx] = value;
202+
++size;
203+
return true;
204+
}
205+
206+
bool deQueue() {
207+
if (isEmpty()) return false;
208+
front = (front + 1) % capacity;
209+
--size;
210+
return true;
211+
}
212+
213+
int Front() {
214+
if (isEmpty()) return -1;
215+
return q[front];
216+
}
217+
218+
int Rear() {
219+
if (isEmpty()) return -1;
220+
int idx = (front + size - 1) % capacity;
221+
return q[idx];
222+
}
223+
224+
bool isEmpty() {
225+
return size == 0;
226+
}
227+
228+
bool isFull() {
229+
return size == capacity;
230+
}
231+
};
232+
233+
/**
234+
* Your MyCircularQueue object will be instantiated and called as such:
235+
* MyCircularQueue* obj = new MyCircularQueue(k);
236+
* bool param_1 = obj->enQueue(value);
237+
* bool param_2 = obj->deQueue();
238+
* int param_3 = obj->Front();
239+
* int param_4 = obj->Rear();
240+
* bool param_5 = obj->isEmpty();
241+
* bool param_6 = obj->isFull();
242+
*/
243+
```
244+
245+
### **Go**
246+
247+
```go
248+
type MyCircularQueue struct {
249+
front int
250+
size int
251+
capacity int
252+
q []int
253+
}
254+
255+
func Constructor(k int) MyCircularQueue {
256+
q := make([]int, k)
257+
return MyCircularQueue{0, 0, k, q}
258+
}
259+
260+
func (this *MyCircularQueue) EnQueue(value int) bool {
261+
if this.IsFull() {
262+
return false
263+
}
264+
idx := (this.front + this.size) % this.capacity
265+
this.q[idx] = value
266+
this.size++
267+
return true
268+
}
269+
270+
func (this *MyCircularQueue) DeQueue() bool {
271+
if this.IsEmpty() {
272+
return false
273+
}
274+
this.front = (this.front + 1) % this.capacity
275+
this.size--
276+
return true
277+
}
278+
279+
func (this *MyCircularQueue) Front() int {
280+
if this.IsEmpty() {
281+
return -1
282+
}
283+
return this.q[this.front]
284+
}
285+
286+
func (this *MyCircularQueue) Rear() int {
287+
if this.IsEmpty() {
288+
return -1
289+
}
290+
idx := (this.front + this.size - 1) % this.capacity
291+
return this.q[idx]
292+
}
293+
294+
func (this *MyCircularQueue) IsEmpty() bool {
295+
return this.size == 0
296+
}
297+
298+
func (this *MyCircularQueue) IsFull() bool {
299+
return this.size == this.capacity
300+
}
301+
302+
/**
303+
* Your MyCircularQueue object will be instantiated and called as such:
304+
* obj := Constructor(k);
305+
* param_1 := obj.EnQueue(value);
306+
* param_2 := obj.DeQueue();
307+
* param_3 := obj.Front();
308+
* param_4 := obj.Rear();
309+
* param_5 := obj.IsEmpty();
310+
* param_6 := obj.IsFull();
311+
*/
312+
```
313+
184314
### **TypeScript**
185315

186316
```ts
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
class MyCircularQueue {
2+
private:
3+
int front;
4+
int size;
5+
int capacity;
6+
vector<int> q;
7+
8+
public:
9+
MyCircularQueue(int k) {
10+
capacity = k;
11+
q = vector<int>(k);
12+
front = size = 0;
13+
}
14+
15+
bool enQueue(int value) {
16+
if (isFull()) return false;
17+
int idx = (front + size) % capacity;
18+
q[idx] = value;
19+
++size;
20+
return true;
21+
}
22+
23+
bool deQueue() {
24+
if (isEmpty()) return false;
25+
front = (front + 1) % capacity;
26+
--size;
27+
return true;
28+
}
29+
30+
int Front() {
31+
if (isEmpty()) return -1;
32+
return q[front];
33+
}
34+
35+
int Rear() {
36+
if (isEmpty()) return -1;
37+
int idx = (front + size - 1) % capacity;
38+
return q[idx];
39+
}
40+
41+
bool isEmpty() {
42+
return size == 0;
43+
}
44+
45+
bool isFull() {
46+
return size == capacity;
47+
}
48+
};
49+
50+
/**
51+
* Your MyCircularQueue object will be instantiated and called as such:
52+
* MyCircularQueue* obj = new MyCircularQueue(k);
53+
* bool param_1 = obj->enQueue(value);
54+
* bool param_2 = obj->deQueue();
55+
* int param_3 = obj->Front();
56+
* int param_4 = obj->Rear();
57+
* bool param_5 = obj->isEmpty();
58+
* bool param_6 = obj->isFull();
59+
*/

0 commit comments

Comments
 (0)