Leetcode学习笔记:#509. Fibonacci Number
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 from 0 and 1. That is,
F(0) = 0, F(1) = 1
F(N) = F(N - 1) + F(N - 2), for N > 1.
Given N, calculate F(N).
实现:
int[] fib_cache = new int[31];
public int fib(int N){
if(N <= 1){
return N;
}
else if(fib_cache[N] != 0){
return fib_cache[N];
}else{
return fib_cahce[N] = fib(N-1) + fib(N-2);
}
}
思路:
动态规划的例题