Skip to content

Commit fcbe599

Browse files
author
zongyanqi
committed
add Easy_70_Climbing_Stairs
1 parent 1773964 commit fcbe599

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

Easy_70_Climbing_Stairs.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* https://leetcode.com/problems/climbing-stairs/#/description
3+
*
4+
* You are climbing a stair case. It takes n steps to reach to the top.
5+
* Each time you can either climb 1 or 2 steps.
6+
* In how many distinct ways can you climb to the top?
7+
*
8+
* Note: Given n will be a positive integer.
9+
*
10+
* 答案:
11+
*
12+
* https://leetcode.com/articles/climbing-stairs/
13+
*/
14+
15+
/**
16+
* @param {number} n
17+
* @return {number}
18+
*/
19+
var climbStairs = function (n) {
20+
if (n < 2) return 1;
21+
return climbStairs(n - 1) + climbStairs(n - 2);
22+
};
23+
24+
/**
25+
* @param {number} n
26+
* @return {number}
27+
*/
28+
var climbStairs2 = function (n) {
29+
var ways = [1, 1];
30+
for (var i = 2; i <= n; i++) {
31+
ways[i] = ways[i - 1] + ways[i - 2];
32+
}
33+
return ways[n];
34+
};
35+
36+
console.log(climbStairs(1));
37+
console.log(climbStairs(2));
38+
console.log(climbStairs(3));
39+
console.log(climbStairs(4));
40+
41+
console.log('==============');
42+
43+
console.log(climbStairs2(1));
44+
console.log(climbStairs2(2));
45+
console.log(climbStairs2(3));
46+
console.log(climbStairs2(4));

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy