509. Fibonacci Number
Question
The Fibonacci numbers, commonly denoted
F(n)form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from0and1. That is,F(0) = 0, F(1) = 1 F(n) = F(n - 1) + F(n - 2), for n > 1.Given
n, calculateF(n).
Solution 1
记忆化搜索,在递归时递归过的n放入数组memo中记录。
Code
1 | class Solution { |
Solution 2
递归,当n等于0或者1时返回固定值。
否则返回fib(n-1)+fib(n-2)。
Code
1 | class Solution { |
509. Fibonacci Number
