Topic one:
We all know that Fibonacci number, and now asked to enter an integer n, you output the n-th Fibonacci number Fibonacci sequence of item (from 0, the first 0 is 0) n <= 39
public int Fibonacci(int n) { if(n==0) return 0; if(n==1) return 1; return Fibonacci(n-1)+Fibonacci(n-2); }
Topic two:
A frog can jump on a Class 1 level, you can also hop on level 2. The frog jumped seeking a total of n grade level how many jumps (the order of different calculation different results).
the n steps: a first step jump, remaining n-1; the first-order Jump 2, n-2 order left
Results f (n) = f (n-1) + f (n-2)
n=1 f(1)=1
n=2 f(2)=2
public int JumpFloor(int n) { if(n==1) return 1; if(n==2) return 2; return JumpFloor(n-1)+JumpFloor(n-2); }
Topic three:
A frog can jump on a Class 1 level, you can also hop on level 2 ...... n It can also jump on stage.
The frog jumped seeking a total of n grade level how many jumps.
Similarly:
f(n)=f(n-1)+...+f(0)
f(n-1)=f(n-2)+...+f(0)
f(n)-f(n-1)=f(n-1)
f(n)=2*f(n-1)
public int JumpFloorII(int target) { if (target <= 0) { return -1; } else if (target == 1) { return 1; } else { return 2 * JumpFloorII(target - 1); } }
Topic Four:
We can use a small rectangle 2 * 1 sideways or vertically to cover a larger rectangle.
Will the small rectangle of n 2 * 1 coverage without overlap a large rectangle 2 * n, a total of how many ways?
Been to Appalachian Fibonacci number is still recommended (see Notes 1, 2 Appalachian consider Fibonacci number)
public int RectCover(int target) { if(target<=0){ return 0; }else if(target<=2){ return target; }else { return RectCover(target-1)+RectCover(target-2); } }