Open Tcdian opened 4 years ago
/**
* @param {number} n
* @return {number}
*/
var climbStairs = function(n) {
const dp = new Array(n + 1);
for (let i = 0; i <= n; i++) {
if (i < 2) {
dp[i] = 1;
} else {
dp[i] = dp[i - 1] + dp[i - 2];
}
}
return dp[n];
};
function climbStairs(n: number): number {
const dp: number[] = new Array(n + 1);
for (let i = 0; i <= n; i++) {
if (i < 2) {
dp[i] = 1;
} else {
dp[i] = dp[i - 1] + dp[i - 2];
}
}
return dp[n];
}
70. Climbing Stairs
假设你正在爬楼梯。需要
n
阶你才能到达楼顶。每次你可以爬
1
或2
个台阶。你有多少种不同的方法可以爬到楼顶呢?Example 1
Example 2
Note
n
是一个正整数