LeetCode 筆記 - 509. Fibonacci Number

題目在此 509. Fibonacci Number

請計算費氏數列的結果

解題思維

就簡單 Dynamic programming 避免重複的計算即可

剩下就跟著定義實作即可

程式碼

1
2
3
4
5
6
7
8
9
10
11
12
class Solution:
count = [inf] * 31
count[0] = 0
count[1] = 1
def fib(self, n: int) -> int:
if self.count[n] != inf:
return self.count[n]

result = self.fib(n - 1) + self.fib(n - 2)
self.count[n] = result

return result

也許你也會想看看