Skip to content

Commit 7fe6578

Browse files
Merge pull request SharingSource#634 from SharingSource/ac_oier
✨feat: add 641
2 parents 1daa06a + b02d37c commit 7fe6578

File tree

2 files changed

+331
-0
lines changed

2 files changed

+331
-0
lines changed

Index/链表.md

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
| [430. 扁平化多级双向链表](https://leetcode-cn.com/problems/flatten-a-multilevel-doubly-linked-list/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/flatten-a-multilevel-doubly-linked-list/solution/gong-shui-san-xie-yi-ti-shuang-jie-di-gu-9wfz/) | 中等 | 🤩🤩🤩🤩🤩 |
2020
| [432. 全 O(1) 的数据结构](https://leetcode-cn.com/problems/all-oone-data-structure/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/all-oone-data-structure/solution/by-ac_oier-t26d/) | 困难 | 🤩🤩🤩🤩 |
2121
| [460. LFU 缓存](https://leetcode-cn.com/problems/lfu-cache/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/lfu-cache/solution/gong-shui-san-xie-yun-yong-tong-pai-xu-s-53m3/) | 困难 | 🤩🤩🤩🤩🤩 |
22+
| [641. 设计循环双端队列](https://leetcode.cn/problems/design-circular-deque/) | [LeetCode 题解链接](https://leetcode.cn/problems/design-circular-deque/solution/by-ac_oier-fwhm/) | 中等 | 🤩🤩🤩🤩🤩 |
2223
| [725. 分隔链表](https://leetcode-cn.com/problems/split-linked-list-in-parts/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/split-linked-list-in-parts/solution/gong-shui-san-xie-jing-dian-lian-biao-ju-9yj4/) | 简单 | 🤩🤩🤩🤩🤩 |
2324
| [1206. 设计跳表](https://leetcode.cn/problems/design-skiplist/) | [LeetCode 题解链接](https://leetcode.cn/problems/design-skiplist/solution/by-ac_oier-38rd/) | 困难 | 🤩🤩🤩🤩 |
2425
| [1600. 皇位继承顺序](https://leetcode-cn.com/problems/throne-inheritance/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/throne-inheritance/solution/gong-shui-san-xie-shi-yong-dan-xiang-lia-7t65/) | 中等 | 🤩🤩🤩 |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,330 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[641. 设计循环双端队列](https://leetcode.cn/problems/design-circular-deque/solution/by-ac_oier-fwhm/)** ,难度为 **中等**
4+
5+
Tag : 「数组」、「链表」、「数据结构」
6+
7+
8+
9+
设计实现双端队列。
10+
11+
实现 `MyCircularDeque` 类:
12+
13+
* `MyCircularDeque(int k)` :构造函数,双端队列最大为 $k$ 。
14+
* `boolean insertFront()`:将一个元素添加到双端队列头部。 如果操作成功返回 `true` ,否则返回 `false`
15+
* `boolean insertLast()` :将一个元素添加到双端队列尾部。如果操作成功返回 `true` ,否则返回 `false`
16+
* `boolean deleteFront()` :从双端队列头部删除一个元素。 如果操作成功返回 `true` ,否则返回 `false`
17+
* `boolean deleteLast()` :从双端队列尾部删除一个元素。如果操作成功返回 `true` ,否则返回 `false`
18+
* `int getFront()` :从双端队列头部获得一个元素。如果双端队列为空,返回 `-1` 。
19+
* `int getRear()` :获得双端队列的最后一个元素。 如果双端队列为空,返回 `-1`
20+
* `boolean isEmpty()` :若双端队列为空,则返回 `true` ,否则返回 `false`  。
21+
* `boolean isFull()` :若双端队列满了,则返回 `true` ,否则返回 `false`
22+
23+
示例 1:
24+
```
25+
输入
26+
["MyCircularDeque", "insertLast", "insertLast", "insertFront", "insertFront", "getRear", "isFull", "deleteLast", "insertFront", "getFront"]
27+
[[3], [1], [2], [3], [4], [], [], [], [4], []]
28+
29+
输出
30+
[null, true, true, true, false, 2, true, true, true, 4]
31+
32+
解释
33+
MyCircularDeque circularDeque = new MycircularDeque(3); // 设置容量大小为3
34+
circularDeque.insertLast(1); // 返回 true
35+
circularDeque.insertLast(2); // 返回 true
36+
circularDeque.insertFront(3); // 返回 true
37+
circularDeque.insertFront(4); // 已经满了,返回 false
38+
circularDeque.getRear(); // 返回 2
39+
circularDeque.isFull(); // 返回 true
40+
circularDeque.deleteLast(); // 返回 true
41+
circularDeque.insertFront(4); // 返回 true
42+
circularDeque.getFront(); // 返回 4
43+
```
44+
45+
提示:
46+
* $1 <= k <= 1000$
47+
* $0 <= value <= 1000$
48+
* `insertFront``insertLast``deleteFront``deleteLast``getFront``getRear``isEmpty``isFull`  调用次数不大于 `2000` 次
49+
50+
---
51+
52+
### 基本分析
53+
54+
一个基本的实现,需要满足除构造函数以外复杂度为 $O(k)$ 以外,其余操作均为 $O(1)$。
55+
56+
常规实现包含两种方式:**数组实现****链表实现**
57+
58+
其中数组实现可以利用调用次数较小,开成调用次数 $3$ 倍大小,然后从中间开始往两边存储,这样做就不用考虑下标边的界问题;而更为常规的解法是构造一个与限定空间 $k$ 等大的数组,使用两下标并配合坐标转换来做,对于下标自增操作而言,只需要进行「加一取模」即可,而对于下标自减操作,由于考虑负值问题,需要进行「增加限定空间偏移后,进行减一再取模」。
59+
60+
而链表实现则无须考虑额外的下标转换问题,但需要额外定义类。
61+
62+
---
63+
64+
### 数组
65+
66+
使用两坐标 `he``ta` 分别代表队列头和尾(初始值均为 $0$),使用 `cnt` 记录当前队列元素大小,使用 `k` 记录初始化时指定的空间大小。
67+
68+
对各类操作进行逻辑控制:
69+
70+
* `insertFront` 操作:需要对 `he` 进行自减操作,即 `he = (he + k - 1) % k` 为目标位置,同时对 `cnt` 进行自增;
71+
* `insertLast` 操作:需要对 `ta` 进行自增操作,但 `ta` 起始指向是待插入位置,因此 `ta` 为目标位置,随后更新 `ta = (ta + 1) % k`,同时对 `cnt` 进行自增;
72+
* `deleteFront` 操作:需要对 `he` 进行自增操作,直接更新 `he = (he + 1) % k`,同时更新 `cnt` 进行自减;
73+
* `deleteLast` 操作:需要对 `ta` 进行自减操作,更新 `ta = (ta + k - 1) % k`,同时更新 `cnt` 进行自减;
74+
* `getFront` 操作:返回 `nums[he]` 即可, 若 `isFull``True`,返回 `-1`
75+
* `getRear` 操作:返回 `nums[ta - 1]`,由于存在负值问题,需要转换为返回 `nums[(ta + k - 1) % k]``isFull``True`,返回 `-1`
76+
* `isEmpty` 操作:根据 `cnt``k` 的关系进行返回;
77+
* `isFull` 操作:根据 `cnt``k` 的关系进行返回;
78+
79+
Java 代码:
80+
```Java
81+
class MyCircularDeque {
82+
int[] nums;
83+
int he, ta, cnt, k;
84+
public MyCircularDeque(int _k) {
85+
k = _k;
86+
nums = new int[k];
87+
}
88+
public boolean insertFront(int value) {
89+
if (isFull()) return false;
90+
he = (he + k - 1) % k;
91+
nums[he] = value; cnt++;
92+
return true;
93+
}
94+
public boolean insertLast(int value) {
95+
if (isFull()) return false;
96+
nums[ta++] = value; cnt++;
97+
ta %= k;
98+
return true;
99+
}
100+
public boolean deleteFront() {
101+
if (isEmpty()) return false;
102+
he = (he + 1) % k; cnt--;
103+
return true;
104+
}
105+
public boolean deleteLast() {
106+
if (isEmpty()) return false;
107+
ta = (ta + k - 1) % k; cnt--;
108+
return true;
109+
}
110+
public int getFront() {
111+
return isEmpty() ? -1 : nums[he];
112+
}
113+
public int getRear() {
114+
return isEmpty() ? -1 : nums[(ta + k - 1) % k];
115+
}
116+
public boolean isEmpty() {
117+
return cnt == 0;
118+
}
119+
public boolean isFull() {
120+
return cnt == k;
121+
}
122+
}
123+
```
124+
TypeScript 代码:
125+
```TypeScript
126+
class MyCircularDeque {
127+
he = 0; ta = 0; cnt = 0; k = 0;
128+
nums: number[];
129+
constructor(_k: number) {
130+
this.k = _k
131+
this.he = this.ta = this.cnt = 0
132+
this.nums = new Array<number>(this.k)
133+
}
134+
insertFront(value: number): boolean {
135+
if (this.isFull()) return false
136+
this.he = (this.he + this.k - 1) % this.k
137+
this.nums[this.he] = value
138+
this.cnt++
139+
return true
140+
}
141+
insertLast(value: number): boolean {
142+
if (this.isFull()) return false
143+
this.nums[this.ta++] = value
144+
this.ta %= this.k
145+
this.cnt++
146+
return true
147+
}
148+
deleteFront(): boolean {
149+
if (this.isEmpty()) return false
150+
this.he = (this.he + 1) % this.k
151+
this.cnt--
152+
return true
153+
}
154+
deleteLast(): boolean {
155+
if (this.isEmpty()) return false
156+
this.ta = (this.ta + this.k - 1) % this.k
157+
this.cnt--
158+
return true
159+
}
160+
getFront(): number {
161+
return this.isEmpty() ? -1 : this.nums[this.he]
162+
}
163+
getRear(): number {
164+
return this.isEmpty() ? -1 : this.nums[(this.ta + this.k - 1) % this.k]
165+
}
166+
isEmpty(): boolean {
167+
return this.cnt == 0
168+
}
169+
isFull(): boolean {
170+
return this.cnt == this.k
171+
}
172+
}
173+
```
174+
* 时间复杂度:除在初始化函数中构造容器复杂度为 $O(k)$ 以外,其余操作复杂度均为 $O(1)$
175+
* 空间复杂度:$O(n)$
176+
177+
---
178+
179+
### 链表
180+
181+
创建 `Node` 代指每个操作的元素,使用双向链表来构造循环队列。
182+
183+
各类操作均对应基本的链表操作,不再赘述。
184+
185+
Java 代码:
186+
```Java
187+
class MyCircularDeque {
188+
class Node {
189+
Node prev, next;
190+
int val;
191+
Node (int _val) {
192+
val = _val;
193+
}
194+
}
195+
int cnt, k;
196+
Node he, ta;
197+
public MyCircularDeque(int _k) {
198+
k = _k;
199+
he = new Node(-1); ta = new Node(-1);
200+
he.next = ta; ta.prev = he;
201+
}
202+
public boolean insertFront(int value) {
203+
if (isFull()) return false;
204+
Node node = new Node(value);
205+
node.next = he.next;
206+
node.prev = he;
207+
he.next.prev = node;
208+
he.next = node;
209+
cnt++;
210+
return true;
211+
}
212+
public boolean insertLast(int value) {
213+
if (isFull()) return false;
214+
Node node = new Node(value);
215+
node.next = ta;
216+
node.prev = ta.prev;
217+
ta.prev.next = node;
218+
ta.prev = node;
219+
cnt++;
220+
return true;
221+
}
222+
public boolean deleteFront() {
223+
if (isEmpty()) return false;
224+
he.next.next.prev = he;
225+
he.next = he.next.next;
226+
cnt--;
227+
return true;
228+
}
229+
public boolean deleteLast() {
230+
if (isEmpty()) return false;
231+
ta.prev.prev.next = ta;
232+
ta.prev = ta.prev.prev;
233+
cnt--;
234+
return true;
235+
}
236+
public int getFront() {
237+
return isEmpty() ? -1 : he.next.val;
238+
}
239+
public int getRear() {
240+
return isEmpty() ? -1 : ta.prev.val;
241+
}
242+
public boolean isEmpty() {
243+
return cnt == 0;
244+
}
245+
public boolean isFull() {
246+
return cnt == k;
247+
}
248+
}
249+
```
250+
TypeScript 代码:
251+
```TypeScript
252+
class TNode {
253+
prev: TNode = null; next: TNode = null;
254+
val: number = 0;
255+
constructor(_val: number) {
256+
this.val = _val
257+
}
258+
}
259+
class MyCircularDeque {
260+
he = null; ta = null;
261+
cnt = 0; k = 0;
262+
constructor(_k: number) {
263+
this.cnt = 0; this.k = _k;
264+
this.he = new TNode(-1); this.ta = new TNode(-1);
265+
this.he.next = this.ta
266+
this.ta.prev = this.he
267+
}
268+
insertFront(value: number): boolean {
269+
if (this.isFull()) return false
270+
const node = new TNode(value)
271+
node.next = this.he.next
272+
node.prev = this.he
273+
this.he.next.prev = node
274+
this.he.next = node
275+
this.cnt++
276+
return true
277+
}
278+
insertLast(value: number): boolean {
279+
if (this.isFull()) return false
280+
const node = new TNode(value)
281+
node.next = this.ta
282+
node.prev = this.ta.prev
283+
this.ta.prev.next = node
284+
this.ta.prev = node
285+
this.cnt++
286+
return true
287+
}
288+
deleteFront(): boolean {
289+
if (this.isEmpty()) return false
290+
this.he.next.next.prev = this.he
291+
this.he.next = this.he.next.next
292+
this.cnt--
293+
return true
294+
}
295+
deleteLast(): boolean {
296+
if (this.isEmpty()) return false
297+
this.ta.prev.prev.next = this.ta
298+
this.ta.prev = this.ta.prev.prev
299+
this.cnt--
300+
return true
301+
}
302+
getFront(): number {
303+
return this.isEmpty() ? -1 : this.he.next.val
304+
}
305+
getRear(): number {
306+
return this.isEmpty() ? -1 : this.ta.prev.val
307+
}
308+
isEmpty(): boolean {
309+
return this.cnt == 0
310+
}
311+
isFull(): boolean {
312+
return this.cnt == this.k
313+
}
314+
}
315+
```
316+
* 时间复杂度:所有操作复杂度均为 $O(1)$
317+
* 空间复杂度:$O(n)$
318+
319+
---
320+
321+
### 最后
322+
323+
这是我们「刷穿 LeetCode」系列文章的第 `No.641` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
324+
325+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
326+
327+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
328+
329+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
330+

0 commit comments

Comments
 (0)