509 斐波那契数
class Solution {public int fib(int n) {int f[] = new int[n + 5];f[0] = 0;f[1] = 1;for(int i = 2;i <= n;i++){f[i] = f[i - 1] + f[i - 2];}return f[n];}
}
时间复杂度O(n)
空间复杂度O(n)
70 爬楼梯
class Solution {public int climbStairs(int n) {int f[] = new int[n + 5];f[0] = 0;f[1] = 1;f[2] = 2;for(int i = 3;i <= n;i++){f[i] = f[i - 1] + f[i - 2];}return f[n]; }
}
时间复杂度O(n)
空间复杂度O(n)
746 使用最小花费爬楼梯
class Solution {public int minCostClimbingStairs(int[] cost) {int f[] = new int[cost.length + 1];f[0] = 0;f[1] = 0;for(int i = 2;i <= cost.length;i++){f[i] = Math.min(f[i - 1] + cost[i - 1],f[i - 2] + cost[i - 2]);}return f[cost.length];}
}
时间复杂度O(n)
空间复杂度O(n)