力扣算法学习day26-3
509-斐波那契数
题目
代码实现
class Solution {
public int fib(int n) {
if(n < 2){
return n;
}
int[] result = new int[n+1];
result[0] = 0;
result[1] = 1;
for(int i = 2;i <= n;i++){
result[i] = result[i-1] + result[i-2];
}
return result[n];
}
}
70-爬楼梯
题目
代码实现
class Solution {
int climbStairs(int n,int m) {
int[] dp = new int[n];
dp[0] = 1;
for (int i = 1; i < n; i++) {
if(i < m){
for(int j = 1;j <= i;j++){
dp[i] += dp[i-j];
}
dp[i]++;
} else{
for(int j = 1;j <= m;j++){
dp[i] += dp[i-j];
}
}
}
for(int i = 0;i < n;i++){
System.out.println(dp[i]);
}
return dp[n-1];
}
}
|