-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleetcode_70.cpp
More file actions
41 lines (38 loc) · 938 Bytes
/
Copy pathleetcode_70.cpp
File metadata and controls
41 lines (38 loc) · 938 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// dfs (time exeed)
class Solution {
public:
int climbStairs(int n) {
int cnt = 0;
int head = n;
dfs(n, head, cnt);
return cnt;
}
private:
void dfs(int& n, int& head, int& cnt){
if(head == 0) {
cnt++;
return;
}
if(head < 0) return;
head --;
dfs(n, head, cnt);
head ++;
head -=2;
dfs(n, head, cnt);
head +=2;
}
};
// 피보나치 수열
class Solution {
public:
int climbStairs(int n) {
if(n <= 2) return n;
int a = 1, b = 2;
for(int i = 3; i <= n; i++){
int tmp = a + b;
a = b;
b = tmp;
}
return b;
}
};