Browse Source

算法温习

master
VivimanZhang 3 years ago
parent
commit
fc52284c48
  1. 62
      golang_learn/data_likou/数据流的中位数.go

62
golang_learn/data_likou/数据流的中位数.go

@ -2,7 +2,6 @@ package main
import ( import (
"fmt" "fmt"
"sort"
) )
/*** /***
@ -28,11 +27,19 @@ import (
*/ */
func main() { func main() {
finder := Constructor() finder := Constructor()
finder.AddNum(1) finder.AddNum(-1)
finder.AddNum(2) fmt.Println(finder.Nums, "=>", finder.FindMedian())
fmt.Println(finder.FindMedian()) finder.AddNum(-2)
finder.AddNum(3) fmt.Println(finder.Nums, "=>", finder.FindMedian())
fmt.Println(finder.FindMedian()) finder.AddNum(-3)
fmt.Println(finder.Nums, "=>", finder.FindMedian())
finder.AddNum(-4)
fmt.Println(finder.Nums, "=>", finder.FindMedian())
finder.AddNum(-5)
fmt.Println(finder.Nums, "=>", finder.FindMedian())
finder.AddNum(-6)
fmt.Println(finder.Nums, "=>", finder.FindMedian())
} }
type MedianFinder struct { type MedianFinder struct {
@ -51,8 +58,15 @@ func (this *MedianFinder) AddNum(num int) {
i = append(i, num) i = append(i, num)
this.Nums = append(i, this.Nums...) this.Nums = append(i, this.Nums...)
} else { } else {
a := append(this.Nums[:index], num) // ⚠️注意:存在元素替换问题,在设置 a 时,因为内存存储是连续的,会导致将 b 的值覆盖掉
this.Nums = append(a, this.Nums[index:]...) // a, b := this.Nums[:index], this.Nums[index:]
// this.Nums = append(append(a, num), b...)
// 避免重复,将数组复制
a, b := make([]int, index), make([]int, len(this.Nums)-index)
copy(a, this.Nums[:index])
copy(b, this.Nums[index:])
this.Nums = append(append(a, num), b...)
} }
} }
@ -60,9 +74,9 @@ func (this *MedianFinder) FindMedian() float64 {
i := len(this.Nums) i := len(this.Nums)
ii := i / 2 ii := i / 2
if i%2 == 0 { if i%2 == 0 {
return float64(this.Nums[ii]) return float64(this.Nums[ii]+this.Nums[ii-1]) / 2
} }
return float64(this.Nums[ii]+this.Nums[ii-1]) / 2 return float64(this.Nums[ii])
} }
/** /**
@ -72,24 +86,34 @@ func (this *MedianFinder) FindMedian() float64 {
* param_2 := obj.FindMedian(); * param_2 := obj.FindMedian();
*/ */
// BinaryQuery 二分查找的实现,如果查找成功返回索引;否则返回-1 // BinaryQuery 使用二分查找 对顺序列表进行定位范围,未定位导未知,返回-1
func BinaryQuery(list []int, target int) int { func BinaryQuery(list []int, target int) int {
// 定义并初始化low和high,声明mid // 定义并初始化low和high,声明mid
low, high := 0, len(list)-1 low, high := 0, len(list)
if high == 0 {
// 前提必须进行排序 return -1
sort.Ints(list) }
high -= 1 // 具体下标
lowVal, highVal := list[0], list[high]
if target <= lowVal {
return 0
}
if target >= highVal {
return high + 1
}
// 使用二分查找 对顺序列表进行定位范围,未定位导未知,返回-1
for low <= high { for low <= high {
mid := (low + high) / 2 mid := (low + high) / 2
if list[mid] == target { if high == low {
return mid return low
} else if list[mid] > target { } else if list[mid] > target {
high = mid - 1 high = mid - 1
} else { } else if list[mid] < target {
low = mid + 1 low = mid + 1
} else {
return mid
} }
} }

Loading…
Cancel
Save