Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add solution and test-cases for problem 641 #887

Merged
merged 1 commit into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 30 additions & 17 deletions leetcode/601-700/0641.Design-Circular-Deque/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,41 @@
# [641.Design Circular Deque][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)

## Description
Design your implementation of the circular double-ended queue (deque).

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
```
Implement the `MyCircularDeque` class:

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

## 题解
**Example 1:**

### 思路1
> ...
Design Circular Deque
```go
```

Input
["MyCircularDeque", "insertLast", "insertLast", "insertFront", "insertFront", "getRear", "isFull", "deleteLast", "insertFront", "getFront"]
[[3], [1], [2], [3], [4], [], [], [], [4], []]
Output
[null, true, true, true, false, 2, true, true, true, 4]

Explanation
MyCircularDeque myCircularDeque = new MyCircularDeque(3);
myCircularDeque.insertLast(1); // return True
myCircularDeque.insertLast(2); // return True
myCircularDeque.insertFront(3); // return True
myCircularDeque.insertFront(4); // return False, the queue is full.
myCircularDeque.getRear(); // return 2
myCircularDeque.isFull(); // return True
myCircularDeque.deleteLast(); // return True
myCircularDeque.insertFront(4); // return True
myCircularDeque.getFront(); // return 4
```

## 结语

Expand Down
114 changes: 112 additions & 2 deletions leetcode/601-700/0641.Design-Circular-Deque/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,115 @@
package Solution

func Solution(x bool) bool {
return x
import "container/list"

type MyCircularDeque struct {
k int
list *list.List
}

func Constructor641(k int) MyCircularDeque {
return MyCircularDeque{
k: k,
list: list.New(),
}
}

func (this *MyCircularDeque) InsertFront(value int) bool {
if this.list.Len() == this.k {
return false
}
this.list.PushFront(value)
return true
}

func (this *MyCircularDeque) InsertLast(value int) bool {
if this.list.Len() == this.k {
return false
}
this.list.PushBack(value)
return true
}

func (this *MyCircularDeque) DeleteFront() bool {
if this.list.Len() == 0 {
return false
}
head := this.list.Front()
this.list.Remove(head)
return true
}

func (this *MyCircularDeque) DeleteLast() bool {
if this.list.Len() == 0 {
return false
}
head := this.list.Back()
this.list.Remove(head)
return true

}

func (this *MyCircularDeque) GetFront() int {
if this.list.Len() == 0 {
return -1
}
return this.list.Front().Value.(int)
}

func (this *MyCircularDeque) GetRear() int {
if this.list.Len() == 0 {
return -1
}
return this.list.Back().Value.(int)

}

func (this *MyCircularDeque) IsEmpty() bool {
return this.list.Len() == 0
}

func (this *MyCircularDeque) IsFull() bool {
return this.k == this.list.Len()
}

type op struct {
n string
v int
}

func Solution(n int, opts []op) []any {
c := Constructor641(n)
ans := make([]any, 0)
for _, o := range opts {
if o.n == "if" {
ans = append(ans, c.InsertFront(o.v))
continue
}
if o.n == "il" {
ans = append(ans, c.InsertLast(o.v))
continue
}
if o.n == "df" {
ans = append(ans, c.DeleteFront())
continue
}
if o.n == "dl" {
ans = append(ans, c.DeleteLast())
continue
}
if o.n == "gf" {
ans = append(ans, c.GetFront())
continue
}
if o.n == "gr" {
ans = append(ans, c.GetRear())
continue
}
if o.n == "ie" {
ans = append(ans, c.IsEmpty())
continue
}
ans = append(ans, c.IsFull())
}
return ans
}
21 changes: 11 additions & 10 deletions leetcode/601-700/0641.Design-Circular-Deque/Solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,31 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
n int
opts []op
expect []any
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", 3, []op{
{"il", 1}, {"il", 2}, {"if", 3}, {"if", 4}, {"gr", 0}, {"", 0}, {"dl", 0}, {"if", 4}, {"gf", 0},
}, []any{true, true, true, false, 2, true, true, true, 4}},
}

// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := Solution(c.inputs)
got := Solution(c.n, c.opts)
if !reflect.DeepEqual(got, c.expect) {
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
c.expect, got, c.inputs)
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
c.expect, got, c.n, c.opts)
}
})
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading