diff --git a/Week 01/id_301/circularDeque.go b/Week 01/id_301/circularDeque.go new file mode 100644 index 000000000..14c200e1b --- /dev/null +++ b/Week 01/id_301/circularDeque.go @@ -0,0 +1,119 @@ +package main + +import "fmt" + +type MyCircularDeque struct { + size int + length int + data []int +} + +/** Initialize your data structure here. Set the size of the deque to be k. */ +func Constructor(k int) MyCircularDeque { + var circularDeque MyCircularDeque + circularDeque = *new(MyCircularDeque) + circularDeque.size = k + circularDeque.length = 0 + return circularDeque +} + +/** Adds an item at the front of Deque. Return true if the operation is successful. */ +func (this *MyCircularDeque) InsertFront(value int) bool { + if this.IsFull() { + return false + } + this.data = append([]int{value}, this.data...) + this.length++ + return true +} + +/** Adds an item at the rear of Deque. Return true if the operation is successful. */ +func (this *MyCircularDeque) InsertLast(value int) bool { + if this.IsFull() { + return false + } + this.data = append(this.data, value) + this.length++ + return true +} + +/** Deletes an item from the front of Deque. Return true if the operation is successful. */ +func (this *MyCircularDeque) DeleteFront() bool { + if this.IsEmpty() { + return false + } + if this.length == 1 { + this.data = []int{} + } else { + this.data = this.data[1:] + } + this.length-- + return true +} + +/** Deletes an item from the rear of Deque. Return true if the operation is successful. */ +func (this *MyCircularDeque) DeleteLast() bool { + if this.IsEmpty() { + return false + } + if this.length == 1 { + this.data = []int{} + } else { + this.data = this.data[:this.length-1] + } + this.length-- + return true +} + +/** Get the front item from the deque. */ +func (this *MyCircularDeque) GetFront() int { + if this.IsEmpty() { + return -1 + } + val := this.data[0] + return val +} + +/** Get the last item from the deque. */ +func (this *MyCircularDeque) GetRear() int { + if this.IsEmpty() { + return -1 + } + val := this.data[this.length-1] + return val +} + +/** Checks whether the circular deque is empty or not. */ +func (this *MyCircularDeque) IsEmpty() bool { + if this.length == 0 { + return true + } + return false +} + +/** Checks whether the circular deque is full or not. */ +func (this *MyCircularDeque) IsFull() bool { + if this.size == this.length { + return true + } else { + return false + } +} + +func main() { + c := Constructor(3) + fmt.Println(c.size) + fmt.Println(c.InsertFront(3)) + fmt.Println(c.length) + fmt.Println(c.InsertLast(1)) + fmt.Println(c.length) + fmt.Println(c.InsertLast(10)) + fmt.Println(c.length) + fmt.Println(c.InsertLast(11)) + fmt.Println(c.length) + fmt.Println(c.InsertLast(11)) + fmt.Println(c.length) + //fmt.Println(c.DeleteFront()) + fmt.Println(c.length) + fmt.Println(c.IsFull()) +} diff --git a/Week_01/id_301/linklist/link_node.go b/Week 01/id_301/linklist/link_node.go similarity index 100% rename from Week_01/id_301/linklist/link_node.go rename to Week 01/id_301/linklist/link_node.go diff --git a/Week_01/id_301/main.go b/Week 01/id_301/main.go similarity index 97% rename from Week_01/id_301/main.go rename to Week 01/id_301/main.go index 580e8518a..00b4a7db9 100644 --- a/Week_01/id_301/main.go +++ b/Week 01/id_301/main.go @@ -25,8 +25,10 @@ func main() { //l2.Next.Next.Val = 7 //fmt.Println(l1.Val) //fmt.Println(linklist.MergeTwoLists(l1, l2).Next.Next.Next.Next.Val) - a1 := []int{1, 3, 9, 8, 7} - fmt.Println(TwoSum(a1, 10)) + //a1 := []int{1, 3, 9, 8, 7} + //fmt.Println(TwoSum(a1, 10)) + c := Constructor(3) + fmt.Println(c.InsertFront(3)) } /** @@ -254,3 +256,7 @@ func MergeTwoLists(l1 *linklist.ListNode, l2 *linklist.ListNode) *linklist.ListN } return res } + +/** +收集雨水 +*/