Skip to content

Commit 278797c

Browse files
author
liguanliang1
committed
feat: climbingStairs
1 parent a4de42e commit 278797c

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

leetcode/climbingStairs/index.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// 假设你正在爬楼梯。需要 n 阶你才能到达楼顶。
2+
3+
// 每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可以爬到楼顶呢
4+
5+
/**
6+
* @param {number} n
7+
* @return {number}
8+
*/
9+
10+
// f(n) = f(n-1) + f(n-2)
11+
const climbStairs = function (n) {
12+
let p = 0, q = 0, r = 1;
13+
for (let i = 1; i <= n; ++i) {
14+
//
15+
p = q;
16+
q = r;
17+
r = p + q;
18+
}
19+
return r;
20+
};

0 commit comments

Comments
 (0)