Skip to content

Commit 7e846a4

Browse files
committed
add LeetCode 70. 爬楼梯
1 parent 0c1e819 commit 7e846a4

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

Diff for: DP/LeetCode 70. 爬楼梯.md

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
![](https://imgconvert.csdnimg.cn/aHR0cHM6Ly9jZG4uanNkZWxpdnIubmV0L2doL2Nob2NvbGF0ZTE5OTkvY2RuL2ltZy8yMDIwMDgyODE0NTUyMS5qcGc?x-oss-process=image/format,png)
2+
>仰望星空的人,不应该被嘲笑
3+
4+
## 题目描述
5+
6+
假设你正在爬楼梯。需要 n 阶你才能到达楼顶。
7+
8+
每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可以爬到楼顶呢?
9+
10+
注意:给定 n 是一个正整数。
11+
12+
示例 1:
13+
14+
```javascript
15+
输入: 2
16+
输出: 2
17+
解释: 有两种方法可以爬到楼顶。
18+
1. 1+ 1
19+
2. 2
20+
```
21+
22+
示例 2:
23+
24+
```javascript
25+
输入: 3
26+
输出: 3
27+
解释: 有三种方法可以爬到楼顶。
28+
1. 1+ 1+ 1
29+
2. 1+ 2
30+
3. 2+ 1
31+
```
32+
33+
来源:力扣(LeetCode)
34+
链接:https://leetcode-cn.com/problems/climbing-stairs
35+
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
36+
37+
38+
39+
## 解题思路
40+
41+
到达第n阶楼梯有从n-1阶走一步和从第n-2阶走两步两种情况
42+
43+
```javascript
44+
/**
45+
* @param {number} n
46+
* @return {number}
47+
*/
48+
var climbStairs = function (n) {
49+
let dp = new Array(n);
50+
dp[1] = 1;
51+
dp[2] = 2;
52+
for (let i = 3; i <= n; i++) { // 到达第n阶楼梯有从n-1阶走一步和从第n-2阶走两步两种情况
53+
dp[i] = dp[i - 1] + dp[i - 2];
54+
}
55+
return dp[n];
56+
};
57+
```
58+
59+
60+
61+
## 最后
62+
文章产出不易,还望各位小伙伴们支持一波!
63+
64+
往期精选:
65+
66+
<a href="https://github.com/Chocolate1999/Front-end-learning-to-organize-notes">小狮子前端の笔记仓库</a>
67+
68+
<a href="https://github.com/Chocolate1999/leetcode-javascript">leetcode-javascript:LeetCode 力扣的 JavaScript 解题仓库,前端刷题路线(思维导图)</a>
69+
70+
小伙伴们可以在Issues中提交自己的解题代码,🤝 欢迎Contributing,可打卡刷题,Give a ⭐️ if this project helped you!
71+
72+
73+
<a href="https://yangchaoyi.vip/">访问超逸の博客</a>,方便小伙伴阅读玩耍~
74+
75+
![](https://img-blog.csdnimg.cn/2020090211491121.png#pic_center)
76+
77+
```javascript
78+
学如逆水行舟,不进则退
79+
```
80+
81+

0 commit comments

Comments
 (0)