Skip to content

Commit 5c2ab3a

Browse files
committedMay 21, 2021
feat: add solutions to lc problem: No.0622. Design Circular Queue
1 parent 6af5acc commit 5c2ab3a

File tree

4 files changed

+297
-54
lines changed

4 files changed

+297
-54
lines changed
 

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

+113-2
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,126 @@ circularQueue.Rear(); &nbsp;// 返回 4</pre>
5959
<!-- 这里可写当前语言的特殊实现逻辑 -->
6060

6161
```python
62-
62+
class MyCircularQueue:
63+
64+
def __init__(self, k: int):
65+
self.q = [0] * k
66+
self.front = 0
67+
self.size = 0
68+
self.capacity = k
69+
70+
def enQueue(self, value: int) -> bool:
71+
if self.isFull():
72+
return False
73+
idx = (self.front + self.size) % self.capacity
74+
self.q[idx] = value
75+
self.size += 1
76+
return True
77+
78+
def deQueue(self) -> bool:
79+
if self.isEmpty():
80+
return False
81+
self.front = (self.front + 1) % self.capacity
82+
self.size -= 1
83+
return True
84+
85+
def Front(self) -> int:
86+
if self.isEmpty():
87+
return -1
88+
return self.q[self.front]
89+
90+
def Rear(self) -> int:
91+
if self.isEmpty():
92+
return -1
93+
idx = (self.front + self.size - 1) % self.capacity
94+
return self.q[idx]
95+
96+
def isEmpty(self) -> bool:
97+
return self.size == 0
98+
99+
def isFull(self) -> bool:
100+
return self.size == self.capacity
101+
102+
103+
# Your MyCircularQueue object will be instantiated and called as such:
104+
# obj = MyCircularQueue(k)
105+
# param_1 = obj.enQueue(value)
106+
# param_2 = obj.deQueue()
107+
# param_3 = obj.Front()
108+
# param_4 = obj.Rear()
109+
# param_5 = obj.isEmpty()
110+
# param_6 = obj.isFull()
63111
```
64112

65113
### **Java**
66114

67115
<!-- 这里可写当前语言的特殊实现逻辑 -->
68116

69117
```java
70-
118+
class MyCircularQueue {
119+
private int[] q;
120+
private int front;
121+
private int size;
122+
private int capacity;
123+
124+
public MyCircularQueue(int k) {
125+
q = new int[k];
126+
capacity = k;
127+
}
128+
129+
public boolean enQueue(int value) {
130+
if (isFull()) {
131+
return false;
132+
}
133+
int idx = (front + size) % capacity;
134+
q[idx] = value;
135+
++size;
136+
return true;
137+
}
138+
139+
public boolean deQueue() {
140+
if (isEmpty()) {
141+
return false;
142+
}
143+
front = (front + 1) % capacity;
144+
--size;
145+
return true;
146+
}
147+
148+
public int Front() {
149+
if (isEmpty()) {
150+
return -1;
151+
}
152+
return q[front];
153+
}
154+
155+
public int Rear() {
156+
if (isEmpty()) {
157+
return -1;
158+
}
159+
int idx = (front + size - 1) % capacity;
160+
return q[idx];
161+
}
162+
163+
public boolean isEmpty() {
164+
return size == 0;
165+
}
166+
167+
public boolean isFull() {
168+
return size == capacity;
169+
}
170+
}
171+
172+
/**
173+
* Your MyCircularQueue object will be instantiated and called as such:
174+
* MyCircularQueue obj = new MyCircularQueue(k);
175+
* boolean param_1 = obj.enQueue(value);
176+
* boolean param_2 = obj.deQueue();
177+
* int param_3 = obj.Front();
178+
* int param_4 = obj.Rear();
179+
* boolean param_5 = obj.isEmpty();
180+
* boolean param_6 = obj.isFull();
181+
*/
71182
```
72183

73184
### **...**

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

+113-2
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,124 @@ myCircularQueue.Rear(); // return 4
6262
### **Python3**
6363

6464
```python
65-
65+
class MyCircularQueue:
66+
67+
def __init__(self, k: int):
68+
self.q = [0] * k
69+
self.front = 0
70+
self.size = 0
71+
self.capacity = k
72+
73+
def enQueue(self, value: int) -> bool:
74+
if self.isFull():
75+
return False
76+
idx = (self.front + self.size) % self.capacity
77+
self.q[idx] = value
78+
self.size += 1
79+
return True
80+
81+
def deQueue(self) -> bool:
82+
if self.isEmpty():
83+
return False
84+
self.front = (self.front + 1) % self.capacity
85+
self.size -= 1
86+
return True
87+
88+
def Front(self) -> int:
89+
if self.isEmpty():
90+
return -1
91+
return self.q[self.front]
92+
93+
def Rear(self) -> int:
94+
if self.isEmpty():
95+
return -1
96+
idx = (self.front + self.size - 1) % self.capacity
97+
return self.q[idx]
98+
99+
def isEmpty(self) -> bool:
100+
return self.size == 0
101+
102+
def isFull(self) -> bool:
103+
return self.size == self.capacity
104+
105+
106+
# Your MyCircularQueue object will be instantiated and called as such:
107+
# obj = MyCircularQueue(k)
108+
# param_1 = obj.enQueue(value)
109+
# param_2 = obj.deQueue()
110+
# param_3 = obj.Front()
111+
# param_4 = obj.Rear()
112+
# param_5 = obj.isEmpty()
113+
# param_6 = obj.isFull()
66114
```
67115

68116
### **Java**
69117

70118
```java
71-
119+
class MyCircularQueue {
120+
private int[] q;
121+
private int front;
122+
private int size;
123+
private int capacity;
124+
125+
public MyCircularQueue(int k) {
126+
q = new int[k];
127+
capacity = k;
128+
}
129+
130+
public boolean enQueue(int value) {
131+
if (isFull()) {
132+
return false;
133+
}
134+
int idx = (front + size) % capacity;
135+
q[idx] = value;
136+
++size;
137+
return true;
138+
}
139+
140+
public boolean deQueue() {
141+
if (isEmpty()) {
142+
return false;
143+
}
144+
front = (front + 1) % capacity;
145+
--size;
146+
return true;
147+
}
148+
149+
public int Front() {
150+
if (isEmpty()) {
151+
return -1;
152+
}
153+
return q[front];
154+
}
155+
156+
public int Rear() {
157+
if (isEmpty()) {
158+
return -1;
159+
}
160+
int idx = (front + size - 1) % capacity;
161+
return q[idx];
162+
}
163+
164+
public boolean isEmpty() {
165+
return size == 0;
166+
}
167+
168+
public boolean isFull() {
169+
return size == capacity;
170+
}
171+
}
172+
173+
/**
174+
* Your MyCircularQueue object will be instantiated and called as such:
175+
* MyCircularQueue obj = new MyCircularQueue(k);
176+
* boolean param_1 = obj.enQueue(value);
177+
* boolean param_2 = obj.deQueue();
178+
* int param_3 = obj.Front();
179+
* int param_4 = obj.Rear();
180+
* boolean param_5 = obj.isEmpty();
181+
* boolean param_6 = obj.isFull();
182+
*/
72183
```
73184

74185
### **...**
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,54 @@
11
class MyCircularQueue {
2-
3-
private Integer[] nums;
4-
private int head;
5-
private int tail;
2+
private int[] q;
3+
private int front;
64
private int size;
5+
private int capacity;
76

8-
/** Initialize your data structure here. Set the size of the queue to be k. */
97
public MyCircularQueue(int k) {
10-
this.nums = new Integer[k];
11-
this.head = -1;
12-
this.tail = -1;
13-
this.size = 0;
8+
q = new int[k];
9+
capacity = k;
1410
}
15-
16-
/** Insert an element into the circular queue. Return true if the operation is successful. */
11+
1712
public boolean enQueue(int value) {
1813
if (isFull()) {
1914
return false;
20-
} else if(this.head == this.tail && this.tail == -1){
21-
this.head++;
22-
this.tail++;
23-
nums[this.tail] = value;
24-
} else {
25-
this.tail = (this.tail + 1) % nums.length;
26-
this.nums[this.tail] = value;
2715
}
28-
this.size++;
16+
int idx = (front + size) % capacity;
17+
q[idx] = value;
18+
++size;
2919
return true;
3020
}
31-
32-
/** Delete an element from the circular queue. Return true if the operation is successful. */
21+
3322
public boolean deQueue() {
3423
if (isEmpty()) {
3524
return false;
36-
} else if (this.head == this.tail) {
37-
this.head = -1;
38-
this.tail = -1;
39-
} else {
40-
this.head = (this.head + 1) % this.nums.length;
4125
}
42-
this.size--;
26+
front = (front + 1) % capacity;
27+
--size;
4328
return true;
4429
}
45-
46-
/** Get the front item from the queue. */
30+
4731
public int Front() {
4832
if (isEmpty()) {
4933
return -1;
50-
} else {
51-
return this.nums[this.head];
5234
}
35+
return q[front];
5336
}
54-
55-
/** Get the last item from the queue. */
37+
5638
public int Rear() {
5739
if (isEmpty()) {
5840
return -1;
59-
} else {
60-
return this.nums[this.tail];
6141
}
42+
int idx = (front + size - 1) % capacity;
43+
return q[idx];
6244
}
63-
64-
/** Checks whether the circular queue is empty or not. */
45+
6546
public boolean isEmpty() {
66-
if (this.size == 0) {
67-
return true;
68-
} else {
69-
return false;
70-
}
47+
return size == 0;
7148
}
72-
73-
/** Checks whether the circular queue is full or not. */
49+
7450
public boolean isFull() {
75-
if (this.size == this.nums.length) {
76-
return true;
77-
} else {
78-
return false;
79-
}
51+
return size == capacity;
8052
}
8153
}
8254

@@ -89,4 +61,4 @@ public boolean isFull() {
8961
* int param_4 = obj.Rear();
9062
* boolean param_5 = obj.isEmpty();
9163
* boolean param_6 = obj.isFull();
92-
*/
64+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
class MyCircularQueue:
2+
3+
def __init__(self, k: int):
4+
self.q = [0] * k
5+
self.front = 0
6+
self.size = 0
7+
self.capacity = k
8+
9+
def enQueue(self, value: int) -> bool:
10+
if self.isFull():
11+
return False
12+
idx = (self.front + self.size) % self.capacity
13+
self.q[idx] = value
14+
self.size += 1
15+
return True
16+
17+
def deQueue(self) -> bool:
18+
if self.isEmpty():
19+
return False
20+
self.front = (self.front + 1) % self.capacity
21+
self.size -= 1
22+
return True
23+
24+
def Front(self) -> int:
25+
if self.isEmpty():
26+
return -1
27+
return self.q[self.front]
28+
29+
def Rear(self) -> int:
30+
if self.isEmpty():
31+
return -1
32+
idx = (self.front + self.size - 1) % self.capacity
33+
return self.q[idx]
34+
35+
def isEmpty(self) -> bool:
36+
return self.size == 0
37+
38+
def isFull(self) -> bool:
39+
return self.size == self.capacity
40+
41+
42+
# Your MyCircularQueue object will be instantiated and called as such:
43+
# obj = MyCircularQueue(k)
44+
# param_1 = obj.enQueue(value)
45+
# param_2 = obj.deQueue()
46+
# param_3 = obj.Front()
47+
# param_4 = obj.Rear()
48+
# param_5 = obj.isEmpty()
49+
# param_6 = obj.isFull()

0 commit comments

Comments
 (0)
Please sign in to comment.