File tree Expand file tree Collapse file tree 2 files changed +38
-0
lines changed
leetcode/0509.Fibonacci-Number
website/content/ChapterFour Expand file tree Collapse file tree 2 files changed +38
-0
lines changed Original file line number Diff line number Diff line change @@ -108,3 +108,22 @@ func fib5(N int) int {
108
108
var goldenRatio float64 = float64 ((1 + math .Sqrt (5 )) / 2 )
109
109
return int (math .Round (math .Pow (goldenRatio , float64 (N )) / math .Sqrt (5 )))
110
110
}
111
+
112
+ // 解法七 协程版,但是时间特别慢,不推荐,放在这里只是告诉大家,写 LeetCode 算法题的时候,启动 goroutine 特别慢
113
+ func fib6 (N int ) int {
114
+ return <- fibb (N )
115
+ }
116
+
117
+ func fibb (n int ) <- chan int {
118
+ result := make (chan int )
119
+ go func () {
120
+ defer close (result )
121
+
122
+ if n <= 1 {
123
+ result <- n
124
+ return
125
+ }
126
+ result <- <- fibb (n - 1 ) + <- fibb (n - 2 )
127
+ }()
128
+ return result
129
+ }
Original file line number Diff line number Diff line change @@ -168,6 +168,25 @@ func fib5(N int) int {
168
168
return int (math.Round (math.Pow (goldenRatio, float64 (N)) / math.Sqrt (5 )))
169
169
}
170
170
171
+ // 解法七 协程版,但是时间特别慢,不推荐,放在这里只是告诉大家,写 LeetCode 算法题的时候,启动 goroutine 特别慢
172
+ func fib6 (N int ) int {
173
+ return <- fibb (N)
174
+ }
175
+
176
+ func fibb (n int ) <- chan int {
177
+ result := make (chan int )
178
+ go func () {
179
+ defer close (result)
180
+
181
+ if n <= 1 {
182
+ result <- n
183
+ return
184
+ }
185
+ result <- <- fibb (n-1 ) + <- fibb (n-2 )
186
+ }()
187
+ return result
188
+ }
189
+
171
190
```
172
191
173
192
You can’t perform that action at this time.
0 commit comments