From 8cc23b4c66e744659bb6ccbbfcc9ee394b806f90 Mon Sep 17 00:00:00 2001 From: viviman Date: Fri, 27 Aug 2021 14:34:30 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=8C=96=E4=BB=A3=E7=A0=81-?= =?UTF-8?q?=E5=85=B3=E4=BA=8E=E5=8A=9B=E6=89=A3=E7=AE=97=E6=B3=95=E7=9A=84?= =?UTF-8?q?=E5=86=85=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../data_algorithm/Boyer-Moore算法.go | 13 ---- golang_learn/data_algorithm/KMP算法.go | 17 ----- .../寻找两个正序数组的中位数.go | 69 ++++++++++++++++++ .../暴力匹配.go | 0 golang_learn/data_type/05.切片Slice.go | 70 ++++++++++++++++++- 5 files changed, 138 insertions(+), 31 deletions(-) delete mode 100644 golang_learn/data_algorithm/Boyer-Moore算法.go delete mode 100644 golang_learn/data_algorithm/KMP算法.go create mode 100644 golang_learn/data_likou/寻找两个正序数组的中位数.go rename golang_learn/{data_algorithm => data_likou}/暴力匹配.go (100%) diff --git a/golang_learn/data_algorithm/Boyer-Moore算法.go b/golang_learn/data_algorithm/Boyer-Moore算法.go deleted file mode 100644 index f88287d..0000000 --- a/golang_learn/data_algorithm/Boyer-Moore算法.go +++ /dev/null @@ -1,13 +0,0 @@ -package main - -import "fmt" - -func main() { - fmt.Println("Boyer-Moore算法 开始...") - - fmt.Println('a') - fmt.Println('A') - fmt.Println('Z') - fmt.Println(string(17)) - fmt.Println(91) -} diff --git a/golang_learn/data_algorithm/KMP算法.go b/golang_learn/data_algorithm/KMP算法.go deleted file mode 100644 index 4622019..0000000 --- a/golang_learn/data_algorithm/KMP算法.go +++ /dev/null @@ -1,17 +0,0 @@ -package main - -import "fmt" - -/** -KMP算法中每一次的匹配, - -1、主串的起始位置 = 上一轮匹配的失配位置; -2、模式串的起始位置 = 重复字符结构的下一位字符(无重复字符结构,则模式串的首字符) -*/ -func main() { - fmt.Println("KMP算法 开始...") - strA := "ababcabcacbab" - strB := "abcac" - fmt.Println(strB, strA) - -} diff --git a/golang_learn/data_likou/寻找两个正序数组的中位数.go b/golang_learn/data_likou/寻找两个正序数组的中位数.go new file mode 100644 index 0000000..d22f5b7 --- /dev/null +++ b/golang_learn/data_likou/寻找两个正序数组的中位数.go @@ -0,0 +1,69 @@ +package main + +import ( + "log" + "sort" +) + +/* + * 4. 寻找两个正序数组的中位数 + * 给定两个大小分别为 m 和 n 的正序(从小到大)数组 nums1 和 nums2。请你找出并返回这两个正序数组的 中位数 。 + * + * 示例 1: + * 输入:nums1 = [1,3], nums2 = [2] + * 输出:2.00000 + * 解释:合并数组 = [1,2,3] ,中位数 2 + * + * 示例 2: + * 输入:nums1 = [1,2], nums2 = [3,4] + * 输出:2.50000 + * 解释:合并数组 = [1,2,3,4] ,中位数 (2 + 3) / 2 = 2.5 + * + * 示例 3: + * 输入:nums1 = [0,0], nums2 = [0,0] + * 输出:0.00000 + * + * 示例 4: + * 输入:nums1 = [], nums2 = [1] + * 输出:1.00000 + * + * 示例 5: + * 输入:nums1 = [2], nums2 = [] + * 输出:2.00000 + * + * + * 提示: + * nums1.length == m + * nums2.length == n + * 0 <= m <= 1000 + * 0 <= n <= 1000 + * 1 <= m + n <= 2000 + * -106 <= nums1[i], nums2[i] <= 106 + * + */ +func main() { + nums1a, nums2a := []int{1, 3}, []int{2} + nums1b, nums2b := []int{1, 2}, []int{3, 4} + nums1c, nums2c := []int{0, 0}, []int{0, 0} + nums1d, nums2d := []int{}, []int{1} + nums1e, nums2e := []int{2}, []int{} + + log.Println(findMedianSortedArrays(nums1a, nums2a)) + log.Println(findMedianSortedArrays(nums1b, nums2b)) + log.Println(findMedianSortedArrays(nums1c, nums2c)) + log.Println(findMedianSortedArrays(nums1d, nums2d)) + log.Println(findMedianSortedArrays(nums1e, nums2e)) +} + +func findMedianSortedArrays(nums1, nums2 []int) float64 { + retLen := len(nums1) + len(nums2) + nums1 = append(nums1, nums2...) + sort.Ints(nums1) + + length := retLen / 2 + if retLen%2 == 0 { + return float64(nums1[length-1]+nums1[length]) / 2.0 + } else { + return float64(nums1[length]) + } +} diff --git a/golang_learn/data_algorithm/暴力匹配.go b/golang_learn/data_likou/暴力匹配.go similarity index 100% rename from golang_learn/data_algorithm/暴力匹配.go rename to golang_learn/data_likou/暴力匹配.go diff --git a/golang_learn/data_type/05.切片Slice.go b/golang_learn/data_type/05.切片Slice.go index 74194c7..c2bf5db 100644 --- a/golang_learn/data_type/05.切片Slice.go +++ b/golang_learn/data_type/05.切片Slice.go @@ -1,6 +1,9 @@ package main -import "fmt" +import ( + "fmt" + "sort" +) func main() { fmt.Println("\n ================== 切片Slice 方式一 = 开始 ================== ") @@ -38,6 +41,38 @@ func main() { numbern = append(numbern, 32) printSS(numbern) + fmt.Println("\n=== 切片Slice 拼接 ====") + + fmt.Println("\n=== 切片Slice 排序:对int的从小到大排序 ====") + arr := []int{1, 3, 5, 7, 9, 2, 4, 6, 8, 0} + sort.Ints(arr) + fmt.Println(arr) + + fmt.Println("\n=== 切片Slice 排序:对float的从小到大排序 ====") + arr2 := []float64{1.1, 3.3, 5.5, 7.7, 9.9, 2.2, 4.4, 6.6, 8.8, 0.0} + sort.Float64s(arr2) + fmt.Println(arr2) + + fmt.Println("\n=== 切片Slice 排序:对字符串按照字典序排序 ====") + arr3 := []string{"abc", "a", "ee", "ff", "dd", "cc", "bb"} + sort.Strings(arr3) + fmt.Println(arr3) + + fmt.Println("\n=== 切片Slice 自定义排序:对int的从大到小排序 ====") + arr4 := []int{1, 3, 5, 7, 9, 2, 4, 6, 8, 0} + sort.Sort(IntArray(arr4)) + fmt.Println(arr4) + + fmt.Println("\n=== 切片Slice 自定义排序:对int的从大到小排序 ====") + arr5 := []Student2{ + {"zhangsan", 60}, + {"lisi", 88}, + {"wangwu", 97}, + {"zhaoliu", 54}, + } + sort.Sort(StudentArray2(arr5)) + fmt.Println(arr) + } func printSS(balance []int) { var j int @@ -46,3 +81,36 @@ func printSS(balance []int) { fmt.Printf("\n第%d个元素值为:", j, balance[j]) } } + +type IntArray []int + +func (self IntArray) Len() int { + return len(self) +} + +func (self IntArray) Less(i, j int) bool { + return self[i] > self[j] +} + +func (self IntArray) Swap(i, j int) { + self[i], self[j] = self[j], self[i] +} + +type Student2 struct { + name string + score int +} + +type StudentArray2 []Student2 + +func (self StudentArray2) Len() int { + return len(self) +} + +func (self StudentArray2) Swap(i, j int) { + self[i], self[j] = self[j], self[i] +} + +func (self StudentArray2) Less(i, j int) bool { + return self[i].score > self[j].score +}