Update 509

This commit is contained in:
YDZ
2021-02-03 16:22:37 +08:00
parent 7887621c9b
commit 3665fc17fb
2 changed files with 38 additions and 0 deletions

View File

@ -108,3 +108,22 @@ func fib5(N int) int {
var goldenRatio float64 = float64((1 + math.Sqrt(5)) / 2)
return int(math.Round(math.Pow(goldenRatio, float64(N)) / math.Sqrt(5)))
}
// 解法七 协程版,但是时间特别慢,不推荐,放在这里只是告诉大家,写 LeetCode 算法题的时候,启动 goroutine 特别慢
func fib6(N int) int {
return <-fibb(N)
}
func fibb(n int) <- chan int {
result := make(chan int)
go func() {
defer close(result)
if n <= 1 {
result <- n
return
}
result <- <-fibb(n-1) + <-fibb(n-2)
}()
return result
}