题解 | #dp数组实现跳台阶#
跳台阶
http://www.nowcoder.com/practice/8c82a5b80378478f9484d87d1c5f12a4
跳台阶,0台的时候假设为1,1台的时候为1,2台的时候是1台和0台的和,类似···
function jumpFloor(number)
{
// write code here
let dp = [1,1];
for(let i = 2;i<=number;i++){
dp[i] = dp[i-2]+dp[i-1];
}
return dp[number];
}
module.exports = {
jumpFloor : jumpFloor
};
费波那奇数列,dp初始化,0时为0,1时为1,2时为1,3时为2,4时为3,5时为5···
{
// write code here
let dp0 = 0;
let dp1 = 1;//或者dp = [0,1]
for(let i =2;i<=n;i++){
let tmp = dp1;
dp1 = dp1 + dp0; //dp[i] = dp[i-1]+dp[i-2]
dp0 = tmp;
}
return dp1;//return dp[n];
}