题解 | 跳台阶
跳台阶
https://www.nowcoder.com/practice/8c82a5b80378478f9484d87d1c5f12a4
import java.util.*; public class Solution { /** * 跳 n 级台阶的方法数 等于跳 n-1级台阶方法数 + 跳 n-2 级台阶方法数 * 我们可以用递归求解,也可以用动态规划求解 * 递归就是 f(n) = f(n-1) + f(n-2) * 动态规划就是维护一个数组来保存中间结果,从 n=1 开始,到 n=number 结束 */ public int jumpFloor(int number) { if (number <= 1) { return 1; } int[] arr = new int[50]; arr[0] = 1; arr[1] = 1; for (int i = 2; i <= number; i++) { arr[i] = arr[i - 1] + arr[i - 2]; } return arr[number]; } }