Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitfcbe599

Browse files
author
zongyanqi
committed
add Easy_70_Climbing_Stairs
1 parent1773964 commitfcbe599

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+
varclimbStairs=function(n){
20+
if(n<2)return1;
21+
returnclimbStairs(n-1)+climbStairs(n-2);
22+
};
23+
24+
/**
25+
*@param {number} n
26+
*@return {number}
27+
*/
28+
varclimbStairs2=function(n){
29+
varways=[1,1];
30+
for(vari=2;i<=n;i++){
31+
ways[i]=ways[i-1]+ways[i-2];
32+
}
33+
returnways[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)

[8]ページ先頭

©2009-2025 Movatter.jp