Title Description
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.
analysis
With Fibonacci number, 1. 2. recursive solving dynamic programming
public class JumpFloor {
public static void main(String[] args) {
System.out.println(jump1(3));
System.out.println(jump1(5));
System.out.println(jump2(3));
System.out.println(jump2(5));
}
//1.递归求解
public static int jump1(int n) {
if (n <= 2) return n;
return jump1(n - 1) + jump1(n - 2);
}
//2.动态规划
public static int jump2(int n) {
if (n <= 2) return n;
int result = 0;
int pre2 = 1;
int pre1 = 2;
for (int i = 3; i <= n; i++) {
result = pre1 + pre2;
pre2 = pre1;
pre1 = result;
}
return result;
}
}
Reproduced in: https: //www.jianshu.com/p/5a808473e855