mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-06 09:23:19 +08:00
Update 509
This commit is contained in:
@ -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
|
||||
}
|
Reference in New Issue
Block a user