Skip to content

Commit

Permalink
Add Biweekly 40
Browse files Browse the repository at this point in the history
  • Loading branch information
halfrost committed Dec 1, 2020
1 parent efbd8e4 commit 1d5b343
Show file tree
Hide file tree
Showing 2 changed files with 185 additions and 0 deletions.
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();
*/
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
}

0 comments on commit 1d5b343

Please sign in to comment.