Skip to content

Commit 399e43a

Browse files
committed
Add solution and test-cases for problem 641
1 parent 09a1dcb commit 399e43a

File tree

3 files changed

+153
-29
lines changed

3 files changed

+153
-29
lines changed

leetcode/601-700/0641.Design-Circular-Deque/README.md

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,41 @@
11
# [641.Design Circular Deque][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
Design your implementation of the circular double-ended queue (deque).
75

8-
**Example 1:**
9-
10-
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13-
```
6+
Implement the `MyCircularDeque` class:
147

15-
## 题意
16-
> ...
8+
- `MyCircularDeque(int k)` Initializes the deque with a maximum size of `k`.
9+
- `boolean insertFront()` Adds an item at the front of Deque. Returns `true` if the operation is successful, or `false` otherwise.
10+
- `boolean insertLast()` Adds an item at the rear of Deque. Returns `true` if the operation is successful, or `false` otherwise.
11+
- `boolean deleteFront()` Deletes an item from the front of Deque. Returns `true` if the operation is successful, or `false` otherwise.
12+
- `boolean deleteLast()` Deletes an item from the rear of Deque. Returns `true` if the operation is successful, or `false` otherwise.
13+
- `int getFront()` Returns the front item from the Deque. Returns `-1` if the deque is empty.
14+
- `int getRear()` Returns the last item from Deque. Returns `-1` if the deque is empty.
15+
- `boolean isEmpty()` Returns `true` if the deque is empty, or `false` otherwise.
16+
- `boolean isFull()` Returns `true` if the deque is full, or `false` otherwise.
1717

18-
## 题解
18+
**Example 1:**
1919

20-
### 思路1
21-
> ...
22-
Design Circular Deque
23-
```go
2420
```
25-
21+
Input
22+
["MyCircularDeque", "insertLast", "insertLast", "insertFront", "insertFront", "getRear", "isFull", "deleteLast", "insertFront", "getFront"]
23+
[[3], [1], [2], [3], [4], [], [], [], [4], []]
24+
Output
25+
[null, true, true, true, false, 2, true, true, true, 4]
26+
27+
Explanation
28+
MyCircularDeque myCircularDeque = new MyCircularDeque(3);
29+
myCircularDeque.insertLast(1); // return True
30+
myCircularDeque.insertLast(2); // return True
31+
myCircularDeque.insertFront(3); // return True
32+
myCircularDeque.insertFront(4); // return False, the queue is full.
33+
myCircularDeque.getRear(); // return 2
34+
myCircularDeque.isFull(); // return True
35+
myCircularDeque.deleteLast(); // return True
36+
myCircularDeque.insertFront(4); // return True
37+
myCircularDeque.getFront(); // return 4
38+
```
2639

2740
## 结语
2841

Lines changed: 112 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,115 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
import "container/list"
4+
5+
type MyCircularDeque struct {
6+
k int
7+
list *list.List
8+
}
9+
10+
func Constructor641(k int) MyCircularDeque {
11+
return MyCircularDeque{
12+
k: k,
13+
list: list.New(),
14+
}
15+
}
16+
17+
func (this *MyCircularDeque) InsertFront(value int) bool {
18+
if this.list.Len() == this.k {
19+
return false
20+
}
21+
this.list.PushFront(value)
22+
return true
23+
}
24+
25+
func (this *MyCircularDeque) InsertLast(value int) bool {
26+
if this.list.Len() == this.k {
27+
return false
28+
}
29+
this.list.PushBack(value)
30+
return true
31+
}
32+
33+
func (this *MyCircularDeque) DeleteFront() bool {
34+
if this.list.Len() == 0 {
35+
return false
36+
}
37+
head := this.list.Front()
38+
this.list.Remove(head)
39+
return true
40+
}
41+
42+
func (this *MyCircularDeque) DeleteLast() bool {
43+
if this.list.Len() == 0 {
44+
return false
45+
}
46+
head := this.list.Back()
47+
this.list.Remove(head)
48+
return true
49+
50+
}
51+
52+
func (this *MyCircularDeque) GetFront() int {
53+
if this.list.Len() == 0 {
54+
return -1
55+
}
56+
return this.list.Front().Value.(int)
57+
}
58+
59+
func (this *MyCircularDeque) GetRear() int {
60+
if this.list.Len() == 0 {
61+
return -1
62+
}
63+
return this.list.Back().Value.(int)
64+
65+
}
66+
67+
func (this *MyCircularDeque) IsEmpty() bool {
68+
return this.list.Len() == 0
69+
}
70+
71+
func (this *MyCircularDeque) IsFull() bool {
72+
return this.k == this.list.Len()
73+
}
74+
75+
type op struct {
76+
n string
77+
v int
78+
}
79+
80+
func Solution(n int, opts []op) []any {
81+
c := Constructor641(n)
82+
ans := make([]any, 0)
83+
for _, o := range opts {
84+
if o.n == "if" {
85+
ans = append(ans, c.InsertFront(o.v))
86+
continue
87+
}
88+
if o.n == "il" {
89+
ans = append(ans, c.InsertLast(o.v))
90+
continue
91+
}
92+
if o.n == "df" {
93+
ans = append(ans, c.DeleteFront())
94+
continue
95+
}
96+
if o.n == "dl" {
97+
ans = append(ans, c.DeleteLast())
98+
continue
99+
}
100+
if o.n == "gf" {
101+
ans = append(ans, c.GetFront())
102+
continue
103+
}
104+
if o.n == "gr" {
105+
ans = append(ans, c.GetRear())
106+
continue
107+
}
108+
if o.n == "ie" {
109+
ans = append(ans, c.IsEmpty())
110+
continue
111+
}
112+
ans = append(ans, c.IsFull())
113+
}
114+
return ans
5115
}

leetcode/601-700/0641.Design-Circular-Deque/Solution_test.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,31 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
n int
14+
opts []op
15+
expect []any
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", 3, []op{
18+
{"il", 1}, {"il", 2}, {"if", 3}, {"if", 4}, {"gr", 0}, {"", 0}, {"dl", 0}, {"if", 4}, {"gf", 0},
19+
}, []any{true, true, true, false, 2, true, true, true, 4}},
1920
}
2021

2122
// 开始测试
2223
for i, c := range cases {
2324
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
25+
got := Solution(c.n, c.opts)
2526
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
27+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
28+
c.expect, got, c.n, c.opts)
2829
}
2930
})
3031
}
3132
}
3233

33-
// 压力测试
34+
// 压力测试
3435
func BenchmarkSolution(b *testing.B) {
3536
}
3637

37-
// 使用案列
38+
// 使用案列
3839
func ExampleSolution() {
3940
}

0 commit comments

Comments
 (0)