Skip to content

Commit

Permalink
add: 爬楼梯
Browse files Browse the repository at this point in the history
  • Loading branch information
xldeng committed Mar 6, 2022
1 parent 6d8733e commit fa3a939
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
3. [在字符串中查找数组中的级联子串](https://github.com/dengshasha/algorithm-study/blob/master/string/substrWithConcatenation.js) || [leetcode地址](https://leetcode.com/problems/substring-with-concatenation-of-all-words/submissions/)
4. [实现atoi函数](https://github.com/dengshasha/algorithm-study/blob/master/string/strToInteger.js) || [leetcode地址](https://leetcode.com/problems/string-to-integer-atoi/submissions/)
5. [电话号码中的字符组合](https://github.com/dengshasha/algorithm-study/blob/master/string/letterCombOfPhoneNum.js) || [leetcode地址](https://leetcode.com/problems/letter-combinations-of-a-phone-number/submissions/)
6. [爬楼梯](https://github.com/dengshasha/algorithm-study/blob/master/string/climbStairs.js) || [leetcode地址](https://leetcode.com/problems/climbing-stairs/)

### 数组
1. [数组各项,除该项外累乘的结果](https://github.com/dengshasha/algorithm-study/blob/master/array/productArrayExceptSelf.js) || [leetcode地址](https://leetcode.com/problems/product-of-array-except-self/submissions/)
Expand Down Expand Up @@ -100,6 +101,7 @@
8. [字符串是否能由给出的字典中的K个子串构成](https://github.com/dengshasha/algorithm-study/blob/master/dynamicProgramming/wordBreak.js) || [leetcode地址](https://leetcode.com/problems/word-break/submissions/)
9. [实现正则表达式匹配](https://github.com/dengshasha/algorithm-study/blob/master/dynamicProgramming/regExpMatch.js) || [leetcode地址](https://leetcode.com/problems/regular-expression-matching/solution/)
10. [实现正则表达式匹配II](https://github.com/dengshasha/algorithm-study/blob/master/dynamicProgramming/wildcardMatch.js) || [leetcode地址](https://leetcode.com/problems/wildcard-matching/submissions/)
11. [爬楼梯](https://github.com/dengshasha/algorithm-study/blob/master/dynamicProgramming/climbStairs.js) || [leetcode地址](https://leetcode.com/problems/climbing-stairs/)
### 找不到分类
1. [实现一个最近最少使用的缓存结构](https://github.com/dengshasha/algorithm-study/blob/master/others/LRUCache.js)

Expand Down
15 changes: 15 additions & 0 deletions dynamicProgramming/climbStairs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* @param {number} n
* @return {number}
* leetcode address: https://leetcode.com/problems/climbing-stairs/
*/
var climbStairs = function(n) {
let dp = [0,1,2]
if(n <= 2) return dp[n]
for(let i = 3; i <= n; i++) {
dp[i] = dp[i-1] + dp[i-2]
}
return dp[n]
};

console.log(climbStairs(44))
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
<title>Document</title>
</head>
<body>
<script src="string/letterCombOfPhoneNum.js"></script>
<script src="dynamicProgramming/climbStairs.js"></script>
</body>
</html>
28 changes: 28 additions & 0 deletions string/climbStairs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* @param {number} n
* @return {number}
* leetcode address: https://leetcode.com/problems/climbing-stairs/
*/
var climbStairs = function(n) {
let way = 0;
let memo = {}
recursive(n, n)
function recursive(target, n) {
if(n < 0) return;
if(n === 0) {
way++;
memo[target] = way;
return;
}
if(memo[n]) {
way += memo[n]
memo[target] = way;
return;
}
recursive(n, n-1)
recursive(n, n-2)
}
return way
};

console.log(climbStairs(7))

0 comments on commit fa3a939

Please sign in to comment.