新增70.爬楼梯完全背包版本 JavaScript版本

This commit is contained in:
jerryfishcode 2021-09-27 21:23:55 +08:00 committed by GitHub
parent 227bed3396
commit 66b3cac2c7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 14 additions and 0 deletions

View File

@ -186,6 +186,20 @@ func climbStairs(n int) int {
}
```
JavaScript:
```javascript
var climbStairs = function(n) {
const dp = new Array(n+1).fill(0);
const weight = [1,2];
dp[0] = 1;
for(let i = 0; i <= n; i++){ //先遍历背包
for(let j = 0; j < weight.length; j++){ // 再遍历物品
if(i >= weight[j]) dp[i] += dp[i-weight[j]];
}
}
return dp[n];
};
```
-----------------------