From 399e43a7a8bc666411ca6ebcdbc3163e29c618af Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Mon, 27 May 2024 09:03:00 +0800 Subject: [PATCH] Add solution and test-cases for problem 641 --- .../0641.Design-Circular-Deque/README.md | 47 +++++--- .../0641.Design-Circular-Deque/Solution.go | 114 +++++++++++++++++- .../Solution_test.go | 21 ++-- 3 files changed, 153 insertions(+), 29 deletions(-) diff --git a/leetcode/601-700/0641.Design-Circular-Deque/README.md b/leetcode/601-700/0641.Design-Circular-Deque/README.md index ad96c5416..e1ef5fc80 100644 --- a/leetcode/601-700/0641.Design-Circular-Deque/README.md +++ b/leetcode/601-700/0641.Design-Circular-Deque/README.md @@ -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 +``` ## 结语 diff --git a/leetcode/601-700/0641.Design-Circular-Deque/Solution.go b/leetcode/601-700/0641.Design-Circular-Deque/Solution.go index d115ccf5e..abece2a92 100644 --- a/leetcode/601-700/0641.Design-Circular-Deque/Solution.go +++ b/leetcode/601-700/0641.Design-Circular-Deque/Solution.go @@ -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 } diff --git a/leetcode/601-700/0641.Design-Circular-Deque/Solution_test.go b/leetcode/601-700/0641.Design-Circular-Deque/Solution_test.go index 14ff50eb4..3012a35a4 100644 --- a/leetcode/601-700/0641.Design-Circular-Deque/Solution_test.go +++ b/leetcode/601-700/0641.Design-Circular-Deque/Solution_test.go @@ -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() { }