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

Commitcabffc3

Browse files
authored
Update climbing_stairs.cpp
1 parent6243d9c commitcabffc3

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

‎cpp/neetcode_150/13_1-d_dynamic_programming/climbing_stairs.cpp

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
Space: O(1)
1111
*/
1212

13-
classSolution {
13+
classSolutionBottomUp {
1414
public:
1515
//Fibonacci series : 'one' stores ways from [n-2]
1616
//'two' stores ways from [n-1]
@@ -24,3 +24,29 @@ class Solution {
2424
return one;
2525
}
2626
};
27+
28+
29+
classSolutionTopDown {
30+
public:
31+
intclimbStairs(int n) {
32+
if (n ==1) {
33+
return1;
34+
}
35+
if (n ==2) {
36+
return2;
37+
}
38+
39+
int first =1;
40+
int second =2;
41+
42+
int result =0;
43+
44+
for (int i =2; i < n; i++) {
45+
result = first + second;
46+
first = second;
47+
second = result;
48+
}
49+
50+
return result;
51+
}
52+
};

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp