-
Notifications
You must be signed in to change notification settings - Fork 0
/
0070.cpp
65 lines (61 loc) · 1.21 KB
/
0070.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
#include <vector>
using namespace std;
class Solution
{
public:
/*
//方法一 经典的斐波那契数列题
int climbStairs(int n)
{
if (n <= 2)
return n;
vector<int> dp(n + 1);
dp[1] = 1, dp[2] = 2;
for (int i = 3; i <= n; i++)
{
dp[i] = dp[i - 1] + dp[i - 2]; //dp[1] = 1, dp[2] = 2
}
return dp[n];
}
*/
/*
//方法二 经典的斐波那契数列题 优化空间 只用两个变量存储值
int climbStairs(int n)
{
if (n <= 2)
return n;
int a = 2, b = 1;
int methods = 0;
for (int i = 3; i <= n; i++)
{
methods = a + b;
b = a;
a = methods;
}
return methods;
}
*/
int climbStairs(int n)
{
if (n == 0 || n == 1)
return n;
vector<int> dp(n + 1);
dp[0] = 1;
dp[1] = 1;
cout << dp[0] << " " << dp[1] << " ";
for (int i = 2; i <= n; i++)
{
dp[i] = dp[i - 1] + dp[i - 2];
cout << dp[i] << " ";
}
return dp[n];
}
};
int main()
{
int n = 3;
Solution S;
S.climbStairs(n);
return 0;
}