-
Notifications
You must be signed in to change notification settings - Fork 8
/
climbingStairs.cpp
70 lines (50 loc) · 1.43 KB
/
climbingStairs.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
66
67
/*leetcode Question 16: Climbing Stairs
Climbing Stairs
You are climbing a stair case. It takes n steps to reach to the top.
Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
The easiest idea is a Fibonacci number. f(n) = f(n-1) + f(n-2).
The nth stairs is from either n-1th the stair or the n-2th stair.
However recursive is time-consuming. We know that recursion can be written in loop,
the trick here is not construct a length of n array, only three element array is enough.*/
/*DP*/
class Solution {
public:
int climbStairs(int n) {
if (n<=2) return n;
int f1=1;
int f2=2;
for(int i=3;i<=n;i++){
f2=f1+f2;
f1=f2-f1;
}
return f2;
}
};
/*recursive*/
class Solution {
public:
int climbStairs(int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if (n<=2) {return n;}
else {
return climbStairs(n-1) + climbStairs(n-2);
}
}
};
/*loop*/
class Solution {
public:
int climbStairs(int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int s[3]={0,1,2};
if (n<=2) {return s[n];}
int j = 2;
while (true){
j++;
s[(j%3)] = s[((j+1)%3)] + s[((j+2)%3)];
if (j==n) {return s[j%3];}
}
}
};