Skip to content

Commit d088cba

Browse files
committed
feat: add solutions to lc problem: No.0641. Design Circular Deque
1 parent 5c2ab3a commit d088cba

File tree

7 files changed

+570
-21
lines changed

7 files changed

+570
-21
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,8 @@
178178
- [LRU 缓存机制](/solution/0100-0199/0146.Lru%20Cache/README.md)
179179
- [实现 Trie (前缀树)](/solution/0200-0299/0208.Implement%20Trie%20%28Prefix%20Tree%29/README.md)
180180
- [实现 Trie (前缀树) II](/solution/1800-1899/1804.Implement%20Trie%20II%20%28Prefix%20Tree%29/README.md)
181+
- [设计循环队列](/solution/0600-0699/0622.Design%20Circular%20Queue/README.md)
182+
- [设计循环双端队列](/solution/0600-0699/0641.Design%20Circular%20Deque/README.md)
181183
- [设计哈希集合](/solution/0700-0799/0705.Design%20HashSet/README.md)
182184
- [设计哈希映射](/solution/0700-0799/0706.Design%20HashMap/README.md)
183185

README_EN.md

+2
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,8 @@ Complete solutions to [LeetCode](https://leetcode-cn.com/problemset/all/), [LCOF
171171
- [LRU Cache](/solution/0100-0199/0146.Lru%20Cache/README_EN.md)
172172
- [Implement Trie (Prefix Tree)](<solution/0200-0299/0208.Implement%20Trie%20(Prefix%20Tree)/README_EN.md>)
173173
- [Implement Trie II (Prefix Tree)](/solution/1800-1899/1804.Implement%20Trie%20II%20%28Prefix%20Tree%29/README_EN.md)
174+
- [Design Circular Queue](/solution/0600-0699/0622.Design%20Circular%20Queue/README_EN.md)
175+
- [Design Circular Deque](/solution/0600-0699/0641.Design%20Circular%20Deque/README_EN.md)
174176
- [Design HashSet](/solution/0700-0799/0705.Design%20HashSet/README_EN.md)
175177
- [Design HashMap](/solution/0700-0799/0706.Design%20HashMap/README_EN.md)
176178

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

+2
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ circularQueue.Rear(); &nbsp;// 返回 4</pre>
5252

5353
<!-- 这里可写通用的实现逻辑 -->
5454

55+
“循环数组”实现。
56+
5557
<!-- tabs:start -->
5658

5759
### **Python3**

solution/0600-0699/0641.Design Circular Deque/README.md

+188-1
Original file line numberDiff line numberDiff line change
@@ -45,27 +45,214 @@ circularDeque.getFront(); // 返回 4
4545
<li>请不要使用内置的双端队列库。</li>
4646
</ul>
4747

48-
4948
## 解法
5049

5150
<!-- 这里可写通用的实现逻辑 -->
5251

52+
“循环数组”实现。
53+
5354
<!-- tabs:start -->
5455

5556
### **Python3**
5657

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

5960
```python
61+
class MyCircularDeque:
62+
63+
def __init__(self, k: int):
64+
"""
65+
Initialize your data structure here. Set the size of the deque to be k.
66+
"""
67+
self.q = [0] * k
68+
self.front = 0
69+
self.size = 0
70+
self.capacity = k
71+
72+
def insertFront(self, value: int) -> bool:
73+
"""
74+
Adds an item at the front of Deque. Return true if the operation is successful.
75+
"""
76+
if self.isFull():
77+
return False
78+
if not self.isEmpty():
79+
self.front = (self.front - 1 + self.capacity) % self.capacity
80+
self.q[self.front] = value
81+
self.size += 1
82+
return True
83+
84+
def insertLast(self, value: int) -> bool:
85+
"""
86+
Adds an item at the rear of Deque. Return true if the operation is successful.
87+
"""
88+
if self.isFull():
89+
return False
90+
idx = (self.front + self.size) % self.capacity
91+
self.q[idx] = value
92+
self.size += 1
93+
return True
94+
95+
def deleteFront(self) -> bool:
96+
"""
97+
Deletes an item from the front of Deque. Return true if the operation is successful.
98+
"""
99+
if self.isEmpty():
100+
return False
101+
self.front = (self.front + 1) % self.capacity
102+
self.size -= 1
103+
return True
104+
105+
def deleteLast(self) -> bool:
106+
"""
107+
Deletes an item from the rear of Deque. Return true if the operation is successful.
108+
"""
109+
if self.isEmpty():
110+
return False
111+
self.size -= 1
112+
return True
113+
114+
def getFront(self) -> int:
115+
"""
116+
Get the front item from the deque.
117+
"""
118+
if self.isEmpty():
119+
return -1
120+
return self.q[self.front]
121+
122+
def getRear(self) -> int:
123+
"""
124+
Get the last item from the deque.
125+
"""
126+
if self.isEmpty():
127+
return -1
128+
idx = (self.front + self.size - 1) % self.capacity
129+
return self.q[idx]
130+
131+
def isEmpty(self) -> bool:
132+
"""
133+
Checks whether the circular deque is empty or not.
134+
"""
135+
return self.size == 0
136+
137+
def isFull(self) -> bool:
138+
"""
139+
Checks whether the circular deque is full or not.
140+
"""
141+
return self.size == self.capacity
60142

143+
144+
# Your MyCircularDeque object will be instantiated and called as such:
145+
# obj = MyCircularDeque(k)
146+
# param_1 = obj.insertFront(value)
147+
# param_2 = obj.insertLast(value)
148+
# param_3 = obj.deleteFront()
149+
# param_4 = obj.deleteLast()
150+
# param_5 = obj.getFront()
151+
# param_6 = obj.getRear()
152+
# param_7 = obj.isEmpty()
153+
# param_8 = obj.isFull()
61154
```
62155

63156
### **Java**
64157

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

67160
```java
161+
class MyCircularDeque {
162+
private int[] q;
163+
private int front;
164+
private int size;
165+
private int capacity;
166+
167+
/** Initialize your data structure here. Set the size of the deque to be k. */
168+
public MyCircularDeque(int k) {
169+
q = new int[k];
170+
capacity = k;
171+
}
172+
173+
/** Adds an item at the front of Deque. Return true if the operation is successful. */
174+
public boolean insertFront(int value) {
175+
if (isFull()) {
176+
return false;
177+
}
178+
if (!isEmpty()) {
179+
front = (front - 1 + capacity) % capacity;
180+
}
181+
q[front] = value;
182+
++size;
183+
return true;
184+
}
185+
186+
/** Adds an item at the rear of Deque. Return true if the operation is successful. */
187+
public boolean insertLast(int value) {
188+
if (isFull()) {
189+
return false;
190+
}
191+
int idx = (front + size) % capacity;
192+
q[idx] = value;
193+
++size;
194+
return true;
195+
}
196+
197+
/** Deletes an item from the front of Deque. Return true if the operation is successful. */
198+
public boolean deleteFront() {
199+
if (isEmpty()) {
200+
return false;
201+
}
202+
front = (front + 1) % capacity;
203+
--size;
204+
return true;
205+
}
206+
207+
/** Deletes an item from the rear of Deque. Return true if the operation is successful. */
208+
public boolean deleteLast() {
209+
if (isEmpty()) {
210+
return false;
211+
}
212+
--size;
213+
return true;
214+
}
215+
216+
/** Get the front item from the deque. */
217+
public int getFront() {
218+
if (isEmpty()) {
219+
return -1;
220+
}
221+
return q[front];
222+
}
223+
224+
/** Get the last item from the deque. */
225+
public int getRear() {
226+
if (isEmpty()) {
227+
return -1;
228+
}
229+
int idx = (front + size - 1) % capacity;
230+
return q[idx];
231+
}
232+
233+
/** Checks whether the circular deque is empty or not. */
234+
public boolean isEmpty() {
235+
return size == 0;
236+
}
237+
238+
/** Checks whether the circular deque is full or not. */
239+
public boolean isFull() {
240+
return size == capacity;
241+
}
242+
}
68243

244+
/**
245+
* Your MyCircularDeque object will be instantiated and called as such:
246+
* MyCircularDeque obj = new MyCircularDeque(k);
247+
* boolean param_1 = obj.insertFront(value);
248+
* boolean param_2 = obj.insertLast(value);
249+
* boolean param_3 = obj.deleteFront();
250+
* boolean param_4 = obj.deleteLast();
251+
* int param_5 = obj.getFront();
252+
* int param_6 = obj.getRear();
253+
* boolean param_7 = obj.isEmpty();
254+
* boolean param_8 = obj.isFull();
255+
*/
69256
```
70257

71258
### **...**

0 commit comments

Comments
 (0)