-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy path70.cpp
41 lines (41 loc) · 1.04 KB
/
70.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
__________________________________________________________________________________________________
4ms
class Solution {
public:
int climbStairs(int n) {
long long int x=1;
long long int y=2;
long long int ans=0;
if(n==1)return 1;
if(n==2)return 2;
n-=2;
while(n--){
ans=x+y;
x=y;
y=ans;
}
return ans;
}
};
__________________________________________________________________________________________________
8132 kb
static const auto speedup = []() {std::ios::sync_with_stdio(false); std::cin.tie(NULL); return 0; }();
class Solution {
public:
int climbStairs(int n) {
if (n <= 2) return n;
int last_two = 1; //当前格子的前两格
int last_one = 2; //当前格子的前一格
int ret = 0;
int tmp = 3;
while (tmp <= n)
{
ret = last_two + last_one;
last_two = last_one;
last_one = ret;
++tmp;
}
return ret;
}
};
__________________________________________________________________________________________________