Browse Source

初始化代码-关于力扣算法的内容

master
viviman 3 years ago
parent
commit
8cc23b4c66
  1. 13
      golang_learn/data_algorithm/Boyer-Moore算法.go
  2. 17
      golang_learn/data_algorithm/KMP算法.go
  3. 69
      golang_learn/data_likou/寻找两个正序数组的中位数.go
  4. 0
      golang_learn/data_likou/暴力匹配.go
  5. 70
      golang_learn/data_type/05.切片Slice.go

13
golang_learn/data_algorithm/Boyer-Moore算法.go

@ -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)
}

17
golang_learn/data_algorithm/KMP算法.go

@ -1,17 +0,0 @@
package main
import "fmt"
/**
KMP算法中每一次的匹配
1主串的起始位置 = 上一轮匹配的失配位置
2模式串的起始位置 = 重复字符结构的下一位字符无重复字符结构则模式串的首字符
*/
func main() {
fmt.Println("KMP算法 开始...")
strA := "ababcabcacbab"
strB := "abcac"
fmt.Println(strB, strA)
}

69
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])
}
}

0
golang_learn/data_algorithm/暴力匹配.go → golang_learn/data_likou/暴力匹配.go

70
golang_learn/data_type/05.切片Slice.go

@ -1,6 +1,9 @@
package main package main
import "fmt" import (
"fmt"
"sort"
)
func main() { func main() {
fmt.Println("\n ================== 切片Slice 方式一 = 开始 ================== ") fmt.Println("\n ================== 切片Slice 方式一 = 开始 ================== ")
@ -38,6 +41,38 @@ func main() {
numbern = append(numbern, 32) numbern = append(numbern, 32)
printSS(numbern) 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) { func printSS(balance []int) {
var j int var j int
@ -46,3 +81,36 @@ func printSS(balance []int) {
fmt.Printf("\n第%d个元素值为:", j, balance[j]) 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
}

Loading…
Cancel
Save