-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
45 lines (40 loc) · 877 Bytes
/
main.cpp
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
42
43
44
45
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std;
class Solution {
public:
// ¹«Ê½
int climbStairs2(int n){
return (pow((1 + sqrt(5)) / 2, n + 1) - pow((1 - sqrt(5)) / 2, n + 1)) / sqrt(5);
}
// ì³²¨ÀÆõÊýÁÐ
int climbStairs1(int n){
if(n == 1){
return 1;
}
int first = 1;
int second = 2;
for(int i=3; i<=n; i++){
int third = first + second;
first = second;
second = third;
}
return second;
}
int climbStairs(int n) {
vector<int> res{1,1};
for(int i=2; i<n; i++){
res.push_back(res[i-1] + res[i-2]);
}
return res[n-1] + res[n-2];
}
};
int main()
{
Solution s;
cout << s.climbStairs2(4) <<endl;
return 0;
}