From 552889ff2482c5214436cdf4e5a468554f0e2eaf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=B1=E6=AF=85=E9=AA=8F?= Date: Thu, 1 Apr 2021 18:12:09 +0800 Subject: [PATCH] =?UTF-8?q?'=E5=A4=A9=E9=99=8D=E5=AD=97=E7=AC=A6=E4=B8=B2?= =?UTF-8?q?=E8=BD=AC=E6=95=B4=E6=95=B0'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/{ algorithms.iml => algorithms.iml} | 0 BinarySearch/main.go | 104 ++++++++++++++- DataStructure/array/array.go | 9 ++ .../字符串/字符串转换整数 (atoi)/main.go | 69 ++++++++++ .../字符串转换整数 (atoi)/有限状态机/main.go | 16 +++ .../图解数据结构/06.从头到尾打印链表/main.go | 102 +++++++++++++++ .../图解数据结构/09. 用两个栈实现队列/main.go | 120 ++++++++++++++++++ .../图解数据结构/20. 表示数值的字符串/main.go | 13 ++ 8 files changed, 426 insertions(+), 7 deletions(-) rename .idea/{ algorithms.iml => algorithms.iml} (100%) create mode 100644 DataStructure/array/array.go create mode 100644 leetcode/初级算法/字符串/字符串转换整数 (atoi)/main.go create mode 100644 leetcode/初级算法/字符串/字符串转换整数 (atoi)/有限状态机/main.go create mode 100644 leetcode/图解数据结构/06.从头到尾打印链表/main.go create mode 100644 leetcode/图解数据结构/09. 用两个栈实现队列/main.go create mode 100644 leetcode/图解数据结构/20. 表示数值的字符串/main.go diff --git a/.idea/ algorithms.iml b/.idea/algorithms.iml similarity index 100% rename from .idea/ algorithms.iml rename to .idea/algorithms.iml diff --git a/BinarySearch/main.go b/BinarySearch/main.go index 622bf53..61833ca 100644 --- a/BinarySearch/main.go +++ b/BinarySearch/main.go @@ -4,7 +4,7 @@ * @Autor: zhuyijun * @Date: 2021-03-29 09:08:50 * @LastEditors: zhuyijun - * @LastEditTime: 2021-03-29 17:35:47 + * @LastEditTime: 2021-03-30 08:59:23 */ package main @@ -14,20 +14,110 @@ import "fmt" 二分查找算法 */ func BinarySearch(key int, a []int) int { - lo := 0 - hi := len(a) - 1 - for lo < hi { - mid := lo + (hi-lo)/2 + start := 0 + end := len(a) - 1 + for start < end { + mid := start + ((end - start) >> 1) if key < a[mid] { - hi = mid - 1 + end = mid - 1 } else if key > a[mid] { - lo = mid + 1 + start = mid + 1 } else { return mid } } return -1 } + +//查找第一个值等于给定值得元素 +func BinarySearch2(nums []int, value int) int { + length := len(nums) + start := 0 + end := length - 1 + for start <= end { + mid := start + ((end - start) >> 1) + if nums[mid] > value { + end = mid - 1 + } else if nums[mid] < value { + start = mid + 1 + } else { + for mid >= 0 { + if nums[mid-1] == value { + mid-- + } else { + return mid + } + } + } + } + return -1 +} + +// 查找最后一个值等于给定值的元素 +func BinarySearch3(nums []int, value int) int { + start := 0 + end := len(nums) - 1 + for start <= end { + mid := start + ((end - start) >> 1) + if nums[mid] > value { + end = mid - 1 + } else if nums[mid] < value { + start = mid + 1 + } else { + for mid < len(nums) { + if nums[mid+1] == value { + mid++ + } else { + return mid + } + } + } + } + return -1 +} + +// 查找第一个大于等于给定值的元素 +func BinarySearch4(nums []int, value int) int { + start := 0 + end := len(nums) - 1 + for start <= end { + mid := (start + end) >> 1 + if nums[mid] < value { + start = mid + 1 + } else { + for mid >= 0 { + if nums[mid-1] >= value { + mid-- + } else { + return mid + } + } + } + } + return -1 +} + +// 查找最后一个小于等于给定值的元素 +func BinarySearch5(nums []int, value int) int { + start := 0 + end := len(nums) - 1 + for start <= end { + mid := (start + end) >> 1 + if nums[mid] > value { + end = mid - 1 + } else { + for mid < len(nums) { + if nums[mid+1] <= value { + mid++ + } else { + return mid + } + } + } + } + return -1 +} + func main() { var a = []int{0, 2, 4, 7, 9, 11, 45, 67, 99, 100} var i int = 2 diff --git a/DataStructure/array/array.go b/DataStructure/array/array.go new file mode 100644 index 0000000..2375114 --- /dev/null +++ b/DataStructure/array/array.go @@ -0,0 +1,9 @@ +/* + * @Description: + * @Version: 2.0 + * @Autor: zhuyijun + * @Date: 2021-03-30 09:01:29 + * @LastEditors: zhuyijun + * @LastEditTime: 2021-03-30 09:01:49 + */ +package array diff --git a/leetcode/初级算法/字符串/字符串转换整数 (atoi)/main.go b/leetcode/初级算法/字符串/字符串转换整数 (atoi)/main.go new file mode 100644 index 0000000..2c32151 --- /dev/null +++ b/leetcode/初级算法/字符串/字符串转换整数 (atoi)/main.go @@ -0,0 +1,69 @@ +package main + +import ( + "math/big" + "strconv" +) + +func myAtoi(s string) int { + const MIN int64 = -2147483648 + const MAX int64 = 2147483647 + ret := big.NewInt(0) + //正负号 + flag := true + size := len(s) + i := 0 + if s == "" { + return 0 + } + //检查空格 + for ; i < size; i++ { + if s[i] != ' ' { + break + } + } + + if i >= size { + return 0 + } + //检查正负号; + if s[i] == '-' || s[i] == '+' { + if s[i] == '-' { + flag = false + } else { + flag = true + } + i++ + } + //检查数字 + for ; i < size; i++ { + //不是数字就跳出 + if s[i] > '9' || s[i] < '0' { + break + } else { + n, err := strconv.ParseInt(string(s[i]),10,64) + if err != nil { + break + } + ret.Mul(ret,big.NewInt(10)) + ret.Add(ret,big.NewInt(n)) + } + } + //添加符号 + if flag == false { + ret.Neg(ret) + } + + if ret.Cmp(big.NewInt(MIN)) < 0 { + ret = big.NewInt(MIN) + } + if ret.Cmp(big.NewInt(MAX)) > 0 { + ret = big.NewInt(MAX) + } + + return int(ret.Int64()) +} + +func main() { + print(myAtoi("9223372036854775808")) +} diff --git a/leetcode/初级算法/字符串/字符串转换整数 (atoi)/有限状态机/main.go b/leetcode/初级算法/字符串/字符串转换整数 (atoi)/有限状态机/main.go new file mode 100644 index 0000000..11bf9f3 --- /dev/null +++ b/leetcode/初级算法/字符串/字符串转换整数 (atoi)/有限状态机/main.go @@ -0,0 +1,16 @@ +package main + +import "strings" + +func myAtoi(s string) int { + //去除首尾字符 + s = strings.TrimSpace(s) + if len(s) == 0 { + return 0 + } + return 0 +} + +func main() { + print(myAtoi("9223372036854775808")) +} diff --git a/leetcode/图解数据结构/06.从头到尾打印链表/main.go b/leetcode/图解数据结构/06.从头到尾打印链表/main.go new file mode 100644 index 0000000..0c5872b --- /dev/null +++ b/leetcode/图解数据结构/06.从头到尾打印链表/main.go @@ -0,0 +1,102 @@ +/* + * @Description: + * @Version: 2.0 + * @Autor: zhuyijun + * @Date: 2021-03-31 10:57:16 + * @LastEditors: zhuyijun + * @LastEditTime: 2021-03-31 11:07:30 + */ + +/** + * Definition for singly-linked list. + * type ListNode struct { + * Val int + * Next *ListNode + * } + */ +package main + +import "fmt" + +type ListNode struct { + Val int + Next *ListNode +} +/** +输入一个链表的头节点,从尾到头反过来返回每个节点的值(用数组返回)。 + +示例 1: +输入:head = [1,3,2] +输出:[2,3,1] + +限制: + +0 <= 链表长度 <= 10000 + */ +//方法1 回溯 +func reversePrint(head *ListNode) []int { + res := make([]int,0) + var reverse func(*ListNode) + reverse = func(node *ListNode) { + if node.Next != nil { + reverse(node.Next) + } + res = append(res, node.Val) + } + if head == nil { + return nil + } + reverse(head) + return res +} +/** +方法二 一般递归 + */ +func reversePrint2(head *ListNode) []int { + if head == nil { + return nil + } + var nums []int + nums = readList(head,nums) + len :=len(nums) + for i:= 0; i < len >> 1; i++{ + temp := nums[i]; + nums[i] = nums[len - i - 1]; + nums[len - i - 1] = temp; + } + return nums +} + +func readList(head *ListNode,nums []int)[]int { + nums = append(nums, head.Val) + print(head.Val) + if head.Next == nil { + return nums + } + return readList(head.Next,nums) +} + +/** +递归回溯 + */ +func reversePrint3(head *ListNode) []int { + if head == nil { + return nil + } + var nums []int + readList3(head,&nums) + return nums +} + +func readList3(head *ListNode,nums* []int) { + if head.Next != nil { + readList3(head.Next,nums) + } + *nums = append(*nums, head.Val) +} + + +func main() { + l := &ListNode{2, &ListNode{1, &ListNode{3, nil}}} + fmt.Printf("%v",reversePrint3(l)) +} diff --git a/leetcode/图解数据结构/09. 用两个栈实现队列/main.go b/leetcode/图解数据结构/09. 用两个栈实现队列/main.go new file mode 100644 index 0000000..8f598d2 --- /dev/null +++ b/leetcode/图解数据结构/09. 用两个栈实现队列/main.go @@ -0,0 +1,120 @@ +package main + +import ( + "container/list" + "fmt" +) + +/* +用两个栈实现一个队列。队列的声明如下,请实现它的两个函数 appendTail 和 deleteHead , +分别完成在队列尾部插入整数和在队列头部删除整数的功能。 +(若队列中没有元素,deleteHead操作返回 -1 ) + +示例 1: +输入: +["CQueue","appendTail","deleteHead","deleteHead"] +[[],[3],[],[]] +输出:[null,null,3,-1] +示例 2: +输入: +["CQueue","deleteHead","appendTail","appendTail","deleteHead","deleteHead"] +[[],[],[5],[2],[],[]] +输出:[null,-1,null,null,5,2] +提示: +1 <= values <= 10000 +最多会对appendTail、deleteHead 进行10000次调用 + + */ +type CQueue struct { + //入 + stack1 []int + //出 + stack2 []int + +} + +func Constructor() CQueue { + return CQueue{stack1: []int{}, stack2: []int{}} +} + + +func (this *CQueue) AppendTail(value int) { + this.stack1 = append(this.stack1[:], value) +} + + +func (this *CQueue) DeleteHead() int { + if len(this.stack2) == 0 && len(this.stack1) == 0 { + return -1 + }else if len(this.stack2) == 0 && len(this.stack1) > 0 { + l := len(this.stack1) - 1 + for len(this.stack1) > 0 { + val := this.stack1[l] + this.stack1 = this.stack1[:l] + l-- + this.stack2 = append(this.stack2,val) + } + } + r := this.stack2[len(this.stack2) - 1] + this.stack2 = this.stack2[:len(this.stack2) - 1] + return r +} + + +//方法二 +type CQueue2 struct { + stack1, stack2 *list.List +} + +func Constructor2() CQueue2 { + return CQueue2{ + stack1: list.New(), + stack2: list.New(), + } +} + +func (this *CQueue2) AppendTail2(value int) { + this.stack1.PushBack(value) +} + +func (this *CQueue2) DeleteHead2() int { + // 如果第二个栈为空 + if this.stack2.Len() == 0 { + for this.stack1.Len() > 0 { + this.stack2.PushBack(this.stack1.Remove(this.stack1.Back())) + } + } + if this.stack2.Len() != 0 { + e := this.stack2.Back() + this.stack2.Remove(e) + return e.Value.(int) + } + return -1 +} + +func main() { + obj := Constructor() + obj.AppendTail(3) + obj.AppendTail(2) + obj.AppendTail(3) + obj.AppendTail(5) + obj.AppendTail(7) + + param_2 := obj.DeleteHead() + fmt.Println(param_2) + + param_2 = obj.DeleteHead() + fmt.Println(param_2) + + param_2 = obj.DeleteHead() + fmt.Println(param_2) + +} +/** + * Your CQueue object will be instantiated and called as such: + * obj := Constructor(); + * obj.AppendTail(value); + * param_2 := obj.DeleteHead(); + */ + + diff --git a/leetcode/图解数据结构/20. 表示数值的字符串/main.go b/leetcode/图解数据结构/20. 表示数值的字符串/main.go new file mode 100644 index 0000000..27858b3 --- /dev/null +++ b/leetcode/图解数据结构/20. 表示数值的字符串/main.go @@ -0,0 +1,13 @@ +package main + +/** +请实现一个函数用来判断字符串是否表示数值(包括整数和小数)。 +例如,字符串"+100"、"5e2"、"-123"、"3.1416"、"-1E-16"、"0123"都表示数值, +但"12e"、"1a3.14"、"1.2.3"、"+-5"及"12e+5.4"都不是。 +*/ + + + +func main() { + +} \ No newline at end of file