Skip to content

Commit

Permalink
add:添加”爬楼梯(跳台阶)“题解
Browse files Browse the repository at this point in the history
  • Loading branch information
hovinghuang committed Apr 17, 2022
1 parent 9b9dab3 commit 69c9262
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Windows or Linux 可以用 (CTRL + click)题目 or 题解 打开一个新窗
| 1* | [无重复字符的最长子串](./src/00-codetop-fontend-100/01-length-of-longest-substring.ts)| 53 | [题目](https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/) [题解](https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/solution/by-hovinghuang-qugo/)| [题目](https://www.nowcoder.com/practice/b56799ebfd684fb394bd315e89324fb4?tpId=295&tqId=1008889&ru=/exam/oj&qru=/ta/format-top101/question-ranking&sourceUrl=%2Fexam%2Foj%3Ftab%3D%25E7%25AE%2597%25E6%25B3%2595%25E7%25AF%2587%26topicId%3D295) [题解](https://blog.nowcoder.net/n/fce41b44190449f18c605a0a41db9c78) | `双指针` `哈希` | `medium` |
| 2* | [合并两个有序数组](./src/00-codetop-fontend-100/02-merge-sorted-arr.ts)| 42 | [题目](https://leetcode-cn.com/problems/merge-sorted-array/) [题解](https://leetcode-cn.com/problems/merge-sorted-array/solution/by-hovinghuang-58f6/)| [题目](https://www.nowcoder.com/practice/89865d4375634fc484f3a24b7fe65665?tpId=295&tqId=658&ru=/exam/oj&qru=/ta/format-top101/question-ranking&sourceUrl=%2Fexam%2Foj%3Ftab%3D%25E7%25AE%2597%25E6%25B3%2595%25E7%25AF%2587%26topicId%3D295) [题解](https://blog.nowcoder.net/n/950d8331e9004749adc1a3d98a5ec29f) | `双指针` | `easy` |
| 3* | [字符串相加](./src/00-codetop-fontend-100/03-add-strings.ts)| 38 | [题目](https://leetcode-cn.com/problems/add-strings/) [题解](https://leetcode-cn.com/problems/add-strings/solution/by-hovinghuang-l59r/)| [题目](https://www.nowcoder.com/practice/11ae12e8c6fe48f883cad618c2e81475?tpId=295&tqId=1061819&ru=/exam/oj&qru=/ta/format-top101/question-ranking&sourceUrl=%2Fexam%2Foj%3Ftab%3D%25E7%25AE%2597%25E6%25B3%2595%25E7%25AF%2587%26topicId%3D295) [题解](https://blog.nowcoder.net/n/1027295c5c8d44f7bbfe2b276e5adf20) | `双指针` `模拟` | `easy` |
| 4* | [比较版本号](./src/00-codetop-fontend-100/04-compare-version.ts)| 37 | [题目](https://leetcode-cn.com/problems/compare-version-numbers/) [题解](https://leetcode-cn.com/problems/compare-version-numbers/solution/by-hovinghuang-rmlf/)| [题目](https://www.nowcoder.com/practice/2b317e02f14247a49ffdbdba315459e7) [题解](https://blog.nowcoder.net/n/5569dbf30e3e45ea977152a629d86439) | `-` | `medium` |
| 4* | [比较版本号](./src/00-codetop-fontend-100/04-compare-version.ts)| 37 | [题目](https://leetcode-cn.com/problems/compare-version-numbers/) [题解](https://leetcode-cn.com/problems/compare-version-numbers/solution/by-hovinghuang-rmlf/)| [题目](https://www.nowcoder.com/practice/2b317e02f14247a49ffdbdba315459e7) [题解](https://blog.nowcoder.net/n/5569dbf30e3e45ea977152a629d86439) | `字符串` | `medium` |
| 5* | [有效的括号](./src/00-codetop-fontend-100/05-is-valid-brackets.ts)| 35 | [题目](https://leetcode-cn.com/problems/valid-parentheses/) [题解](https://leetcode-cn.com/problems/valid-parentheses/solution/by-hovinghuang-6gz1/)| [题目](https://www.nowcoder.com/practice/37548e94a270412c8b9fb85643c8ccc2?tpId=295&tqId=726&ru=/exam/oj&qru=/ta/format-top101/question-ranking&sourceUrl=%2Fexam%2Foj%3Ftab%3D%25E7%25AE%2597%25E6%25B3%2595%25E7%25AF%2587%26topicId%3D295) [题解](https://blog.nowcoder.net/n/620e933e59534064a9722bfa6da0c3d0) | `` | `easy` |
| 6* | [两数之和](./src/00-codetop-fontend-100/06-two-sum.ts)| 33 | [题目](https://leetcode-cn.com/problems/two-sum/) [题解](https://leetcode-cn.com/problems/two-sum/solution/by-hovinghuang-8jqr/)| [题目](https://www.nowcoder.com/practice/20ef0972485e41019e39543e8e895b7f?tpId=295&tqId=745&ru=/exam/oj&qru=/ta/format-top101/question-ranking&sourceUrl=%2Fexam%2Foj%3Ftab%3D%25E7%25AE%2597%25E6%25B3%2595%25E7%25AF%2587%26topicId%3D295) [题解](https://blog.nowcoder.net/n/61a9e5d2ac2f46db9f6f9e324faab016) | `哈希` | `easy`
| 7 | [爬楼梯]()| 25 | [题目]() [题解]()| [题目]() [题解]() | `-` `-` | `easy` |
Expand Down
59 changes: 59 additions & 0 deletions src/00-codetop-fontend-100/07-climb-stairs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// @ts-nocheck
/**
* 题目名称:爬楼梯(跳台阶)
* leetcode 题目: https://leetcode-cn.com/problems/climbing-stairs/
* leetcode 题解: https://leetcode-cn.com/problems/climbing-stairs/solution/by-hovinghuang-bl5r/
* 牛客网 题目: https://www.nowcoder.com/practice/8c82a5b80378478f9484d87d1c5f12a4?tpId=295&tqId=23261&ru=/exam/oj&qru=/ta/format-top101/question-ranking&sourceUrl=%2Fexam%2Foj%3Ftab%3D%25E7%25AE%2597%25E6%25B3%2595%25E7%25AF%2587%26topicId%3D295
* 牛客网 题解: https://blog.nowcoder.net/n/7057ccebe8fd452580442b74ffbb8d90
*/

/**
* 解法一:递归(超时)
* 思路:满足斐波那契数列公式,最简单的肯定是递归
* 时间复杂度:O(2^n)
* 空间复杂度:
*/
function climbStairs(n: number): number {
if (n <= 2) return n
return climbStairs(n - 1) + climbStairs(n - 2)
};

/**
* 解法二:循环(记忆化累加)
* 思路:n1、n2 记录前面两位的结果,一个循环搞定
* 时间复杂度:O(n)
* 空间复杂度:O(1)
*/
function climbStairs(n: number): number {
if (n <= 2) return n

let n1 = 1 // 记录 n - 1 的结果
let n2 = 1 // 记录 n - 2 的结果
let res = 0

for (let i = 2; i <= n; i++) {
res = n1 + n2

// 记录中间结果
n2 = n1
n1 = res
}

return res
};

/**
* 解法三:动态规划
* 思路:
* 时间复杂度:O(n)
* 空间复杂度:
*/
function climbStairs(n: number): number {
const dp = new Array(n + 1)
dp[1] = 1
dp[2] = 2
for (let i = 3; i <= n; i++){
dp[i] = dp[i - 1] + dp[i - 2]
}
return dp[n]
};

0 comments on commit 69c9262

Please sign in to comment.