Skip to content

Commit fbfd5ce

Browse files
authored
Merge pull request #892 from 0xff-dev/622
Add solution and test-cases for problem 622
2 parents d4251e0 + 2d20d3d commit fbfd5ce

File tree

3 files changed

+171
-26
lines changed

3 files changed

+171
-26
lines changed

leetcode/601-700/0622.Design-Circular-Queue/README.md

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,43 @@
11
# [622.Design Circular Queue][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 queue. The circular queue is a linear data structure in which the operations are performed based on FIFO (First In First Out) principle, and the last position is connected back to the first position to make a circle. It is also called "Ring Buffer".
75

8-
**Example 1:**
6+
One of the benefits of the circular queue is that we can make use of the spaces in front of the queue. In a normal queue, once the queue becomes full, we cannot insert the next element even if there is a space in front of the queue. But using the circular queue, we can use the space to store new values.
97

10-
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13-
```
8+
Implement the `MyCircularQueue` class:
149

15-
## 题意
16-
> ...
10+
- `MyCircularQueue(k)` Initializes the object with the size of the queue to be `k`.
11+
- `int Front()` Gets the front item from the queue. If the queue is empty, return `-1`.
12+
- `int Rear()` Gets the last item from the queue. If the queue is empty, return `-1`.
13+
- `boolean enQueue(int value)` Inserts an element into the circular queue. Return `true` if the operation is successful.
14+
- `boolean deQueue()` Deletes an element from the circular queue. Return `true` if the operation is successful.
15+
- `boolean isEmpty()` Checks whether the circular queue is empty or not.
16+
- `boolean isFull()` Checks whether the circular queue is full or not.
1717

18-
## 题解
18+
You must solve the problem without using the built-in queue data structure in your programming language.
1919

20-
### 思路1
21-
> ...
22-
Design Circular Queue
23-
```go
24-
```
20+
**Example 1:**
2521

22+
```
23+
Input
24+
["MyCircularQueue", "enQueue", "enQueue", "enQueue", "enQueue", "Rear", "isFull", "deQueue", "enQueue", "Rear"]
25+
[[3], [1], [2], [3], [4], [], [], [], [4], []]
26+
Output
27+
[null, true, true, true, false, 3, true, true, true, 4]
28+
29+
Explanation
30+
MyCircularQueue myCircularQueue = new MyCircularQueue(3);
31+
myCircularQueue.enQueue(1); // return True
32+
myCircularQueue.enQueue(2); // return True
33+
myCircularQueue.enQueue(3); // return True
34+
myCircularQueue.enQueue(4); // return False
35+
myCircularQueue.Rear(); // return 3
36+
myCircularQueue.isFull(); // return True
37+
myCircularQueue.deQueue(); // return True
38+
myCircularQueue.enQueue(4); // return True
39+
myCircularQueue.Rear(); // return 4
40+
```
2641

2742
## 结语
2843

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

3-
func Solution(x bool) bool {
4-
return x
3+
type node622 struct {
4+
val int
5+
next, pre *node622
6+
}
7+
8+
type node622List struct {
9+
root *node622
10+
count int
11+
}
12+
13+
func (n *node622List) len() int {
14+
return n.count
15+
}
16+
17+
func (n *node622List) pushEnd(v int) {
18+
node := &node622{val: v}
19+
n.count++
20+
if n.count == 1 {
21+
node.next = node
22+
node.pre = node
23+
n.root = node
24+
return
25+
}
26+
n.root.pre.next = node
27+
node.pre = n.root.pre
28+
node.next = n.root
29+
n.root.pre = node
30+
}
31+
32+
func (n *node622List) removeFront() {
33+
n.count--
34+
if n.count == 0 {
35+
n.root = nil
36+
return
37+
}
38+
// a,b,c
39+
newRoot := n.root.next
40+
newRoot.pre = n.root.pre
41+
n.root.pre.next = newRoot
42+
n.root = newRoot
43+
}
44+
45+
func (n *node622List) Front() int {
46+
if n.count == 0 {
47+
return -1
48+
}
49+
return n.root.val
50+
}
51+
func (n *node622List) Back() int {
52+
if n.count == 0 {
53+
return -1
54+
}
55+
return n.root.pre.val
56+
}
57+
58+
type MyCircularQueue struct {
59+
root *node622List
60+
count int
61+
}
62+
63+
func Constructor(k int) MyCircularQueue {
64+
return MyCircularQueue{root: &node622List{}, count: k}
65+
}
66+
67+
func (this *MyCircularQueue) EnQueue(value int) bool {
68+
if this.root.count == this.count {
69+
return false
70+
}
71+
this.root.pushEnd(value)
72+
return true
73+
}
74+
75+
func (this *MyCircularQueue) DeQueue() bool {
76+
if this.root.count == 0 {
77+
return false
78+
}
79+
this.root.removeFront()
80+
return true
81+
}
82+
83+
func (this *MyCircularQueue) Front() int {
84+
return this.root.Front()
85+
}
86+
87+
func (this *MyCircularQueue) Rear() int {
88+
return this.root.Back()
89+
}
90+
91+
func (this *MyCircularQueue) IsEmpty() bool {
92+
return this.root.len() == 0
93+
}
94+
95+
func (this *MyCircularQueue) IsFull() bool {
96+
return this.root.len() == this.count
97+
}
98+
99+
type opt struct {
100+
name string
101+
val int
102+
}
103+
104+
func Solution(n int, opts []opt) []any {
105+
c := Constructor(n)
106+
ans := make([]any, len(opts))
107+
for i, op := range opts {
108+
if op.name == "en" {
109+
ans[i] = c.EnQueue(op.val)
110+
continue
111+
}
112+
if op.name == "de" {
113+
ans[i] = c.DeQueue()
114+
continue
115+
}
116+
if op.name == "fr" {
117+
ans[i] = c.Front()
118+
continue
119+
}
120+
if op.name == "re" {
121+
ans[i] = c.Rear()
122+
continue
123+
}
124+
if op.name == "em" {
125+
ans[i] = c.IsEmpty()
126+
continue
127+
}
128+
ans[i] = c.IsFull()
129+
}
130+
return ans
5131
}

leetcode/601-700/0622.Design-Circular-Queue/Solution_test.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,22 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs int
14+
opts []opt
15+
expect []any
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", 3, []opt{
18+
{"en", 1}, {"en", 2}, {"en", 3},
19+
{"en", 4}, {"re", 0}, {"", 0},
20+
{"de", 0}, {"en", 4}, {"re", 0},
21+
},
22+
[]any{true, true, true, false, 3, true, true, true, 4}},
1923
}
2024

2125
// 开始测试
2226
for i, c := range cases {
2327
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
28+
got := Solution(c.inputs, c.opts)
2529
if !reflect.DeepEqual(got, c.expect) {
2630
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
2731
c.expect, got, c.inputs)
@@ -30,10 +34,10 @@ func TestSolution(t *testing.T) {
3034
}
3135
}
3236

33-
// 压力测试
37+
// 压力测试
3438
func BenchmarkSolution(b *testing.B) {
3539
}
3640

37-
// 使用案列
41+
// 使用案列
3842
func ExampleSolution() {
3943
}

0 commit comments

Comments
 (0)