forked from halfrost/LeetCode-Go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
185 additions
and
0 deletions.
There are no files selected for viewing
78 changes: 78 additions & 0 deletions
78
leetcode/5560.Design-Front-Middle-Back-Queue/5560. Design Front Middle Back Queue.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package leetcode | ||
|
||
type FrontMiddleBackQueue struct { | ||
Queue []int | ||
Length int | ||
} | ||
|
||
func Constructor() FrontMiddleBackQueue { | ||
return FrontMiddleBackQueue{Queue: make([]int, 0), Length: 0} | ||
} | ||
|
||
func (this *FrontMiddleBackQueue) PushFront(val int) { | ||
tmp := make([]int, this.Length+1) | ||
copy(tmp[1:], this.Queue) | ||
tmp[0] = val | ||
this.Queue = tmp | ||
this.Length++ | ||
} | ||
|
||
func (this *FrontMiddleBackQueue) PushMiddle(val int) { | ||
tmp := make([]int, this.Length+1) | ||
idx := this.Length / 2 | ||
copy(tmp[:idx], this.Queue[:idx]) | ||
tmp[idx] = val | ||
copy(tmp[idx+1:], this.Queue[idx:]) | ||
this.Queue = tmp | ||
this.Length++ | ||
} | ||
|
||
func (this *FrontMiddleBackQueue) PushBack(val int) { | ||
this.Queue = append(this.Queue, val) | ||
this.Length++ | ||
} | ||
|
||
func (this *FrontMiddleBackQueue) PopFront() int { | ||
if this.Length == 0 { | ||
return -1 | ||
} | ||
res := this.Queue[0] | ||
this.Queue = this.Queue[1:] | ||
this.Length-- | ||
return res | ||
} | ||
|
||
func (this *FrontMiddleBackQueue) PopMiddle() int { | ||
if this.Length == 0 { | ||
return -1 | ||
} | ||
mid := (this.Length - 1) / 2 | ||
res := this.Queue[mid] | ||
tmp := make([]int, len(this.Queue)-1) | ||
copy(tmp[:mid], this.Queue[:mid]) | ||
copy(tmp[mid:], this.Queue[mid+1:]) | ||
this.Queue = tmp | ||
this.Length-- | ||
return res | ||
} | ||
|
||
func (this *FrontMiddleBackQueue) PopBack() int { | ||
if this.Length == 0 { | ||
return -1 | ||
} | ||
res := this.Queue[this.Length-1] | ||
this.Queue = this.Queue[:this.Length-1] | ||
this.Length-- | ||
return res | ||
} | ||
|
||
/** | ||
* Your FrontMiddleBackQueue object will be instantiated and called as such: | ||
* obj := Constructor(); | ||
* obj.PushFront(val); | ||
* obj.PushMiddle(val); | ||
* obj.PushBack(val); | ||
* param_4 := obj.PopFront(); | ||
* param_5 := obj.PopMiddle(); | ||
* param_6 := obj.PopBack(); | ||
*/ |
107 changes: 107 additions & 0 deletions
107
leetcode/5560.Design-Front-Middle-Back-Queue/5560. Design Front Middle Back Queue_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package leetcode | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
func Test_Problem707(t *testing.T) { | ||
obj := Constructor() | ||
fmt.Printf("obj = %v %v\n", MList2Ints(&obj), obj) | ||
obj.PushFront(1) | ||
fmt.Printf("obj = %v %v\n", MList2Ints(&obj), obj) | ||
obj.PushBack(2) | ||
fmt.Printf("obj = %v\n", MList2Ints(&obj)) | ||
obj.PushMiddle(3) | ||
fmt.Printf("obj = %v\n", MList2Ints(&obj)) | ||
obj.PushMiddle(4) | ||
fmt.Printf("obj = %v\n", MList2Ints(&obj)) | ||
param1 := obj.PopFront() | ||
fmt.Printf("param1 = %v obj = %v\n", param1, MList2Ints(&obj)) | ||
param1 = obj.PopMiddle() | ||
fmt.Printf("param1 = %v obj = %v\n", param1, MList2Ints(&obj)) | ||
param1 = obj.PopMiddle() | ||
fmt.Printf("param1 = %v obj = %v\n", param1, MList2Ints(&obj)) | ||
param1 = obj.PopBack() | ||
fmt.Printf("param1 = %v obj = %v\n", param1, MList2Ints(&obj)) | ||
param1 = obj.PopFront() | ||
fmt.Printf("param1 = %v obj = %v\n", param1, MList2Ints(&obj)) | ||
fmt.Printf("-----------------------------------------------------------------\n") | ||
obj = Constructor() | ||
fmt.Printf("obj = %v %v\n", MList2Ints(&obj), obj) | ||
obj.PushFront(1) | ||
fmt.Printf("obj = %v %v\n", MList2Ints(&obj), obj) | ||
obj.PushFront(2) | ||
fmt.Printf("obj = %v %v\n", MList2Ints(&obj), obj) | ||
obj.PushFront(3) | ||
fmt.Printf("obj = %v %v\n", MList2Ints(&obj), obj) | ||
obj.PushFront(4) | ||
fmt.Printf("obj = %v %v\n", MList2Ints(&obj), obj) | ||
param1 = obj.PopBack() | ||
fmt.Printf("param1 = %v obj = %v\n", param1, MList2Ints(&obj)) | ||
param1 = obj.PopBack() | ||
fmt.Printf("param1 = %v obj = %v\n", param1, MList2Ints(&obj)) | ||
param1 = obj.PopBack() | ||
fmt.Printf("param1 = %v obj = %v\n", param1, MList2Ints(&obj)) | ||
param1 = obj.PopBack() | ||
fmt.Printf("param1 = %v obj = %v\n", param1, MList2Ints(&obj)) | ||
fmt.Printf("-----------------------------------------------------------------\n") | ||
obj = Constructor() | ||
fmt.Printf("obj = %v %v\n", MList2Ints(&obj), obj) | ||
obj.PushMiddle(1) | ||
fmt.Printf("obj = %v\n", MList2Ints(&obj)) | ||
obj.PushMiddle(2) | ||
fmt.Printf("obj = %v\n", MList2Ints(&obj)) | ||
obj.PushMiddle(3) | ||
fmt.Printf("obj = %v\n", MList2Ints(&obj)) | ||
param1 = obj.PopMiddle() | ||
fmt.Printf("param1 = %v obj = %v\n", param1, MList2Ints(&obj)) | ||
param1 = obj.PopMiddle() | ||
fmt.Printf("param1 = %v obj = %v\n", param1, MList2Ints(&obj)) | ||
param1 = obj.PopMiddle() | ||
fmt.Printf("param1 = %v obj = %v\n", param1, MList2Ints(&obj)) | ||
fmt.Printf("-----------------------------------------------------------------\n") | ||
obj = Constructor() | ||
fmt.Printf("obj = %v %v\n", MList2Ints(&obj), obj) | ||
obj.PushMiddle(8) | ||
fmt.Printf("obj = %v\n", MList2Ints(&obj)) | ||
param1 = obj.PopMiddle() | ||
fmt.Printf("param1 = %v obj = %v\n", param1, MList2Ints(&obj)) | ||
param1 = obj.PopFront() | ||
fmt.Printf("param1 = %v obj = %v\n", param1, MList2Ints(&obj)) | ||
param1 = obj.PopBack() | ||
fmt.Printf("param1 = %v obj = %v\n", param1, MList2Ints(&obj)) | ||
param1 = obj.PopMiddle() | ||
fmt.Printf("param1 = %v obj = %v\n", param1, MList2Ints(&obj)) | ||
obj.PushMiddle(1) | ||
fmt.Printf("obj = %v\n", MList2Ints(&obj)) | ||
obj.PushMiddle(10) | ||
fmt.Printf("obj = %v\n", MList2Ints(&obj)) | ||
fmt.Printf("-----------------------------------------------------------------\n") | ||
obj = Constructor() | ||
fmt.Printf("obj = %v %v\n", MList2Ints(&obj), obj) | ||
param1 = obj.PopMiddle() | ||
fmt.Printf("param1 = %v obj = %v\n", param1, MList2Ints(&obj)) | ||
obj.PushMiddle(3) | ||
fmt.Printf("obj = %v %v\n", MList2Ints(&obj), obj) | ||
obj.PushFront(6) | ||
fmt.Printf("obj = %v %v\n", MList2Ints(&obj), obj) | ||
obj.PushMiddle(6) | ||
fmt.Printf("obj = %v %v\n", MList2Ints(&obj), obj) | ||
obj.PushMiddle(3) | ||
fmt.Printf("obj = %v %v\n", MList2Ints(&obj), obj) | ||
param1 = obj.PopMiddle() | ||
fmt.Printf("param1 = %v obj = %v %v\n", param1, MList2Ints(&obj), obj) | ||
obj.PushMiddle(7) | ||
fmt.Printf("obj = %v %v\n", MList2Ints(&obj), obj) | ||
param1 = obj.PopMiddle() | ||
fmt.Printf("param1 = %v obj = %v %v\n", param1, MList2Ints(&obj), obj) | ||
obj.PushMiddle(8) | ||
fmt.Printf("obj = %v %v\n", MList2Ints(&obj), obj) | ||
// ["FrontMiddleBackQueue","popMiddle","pushMiddle","pushFront","pushMiddle","pushMiddle","popMiddle","pushMiddle","popMiddle","pushMiddle"] | ||
// [[],[],[3],[6],[6],[3],[],[7],[],[8]] | ||
} | ||
|
||
func MList2Ints(head *FrontMiddleBackQueue) []int { | ||
return head.Queue | ||
} |