-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClimbingStairs.java
More file actions
33 lines (29 loc) · 627 Bytes
/
Copy pathClimbingStairs.java
File metadata and controls
33 lines (29 loc) · 627 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
class Solution {
public int climbStairs(int n) {
if(n <= 0) return 0;
if(n <= 2) return n;
int[] d = new int[n];
d[0] = 1;
d[1] = 2;
for(int i = 2; i < n; i++) {
d[i] = d[i-1] + d[i-2];
}
return d[n-1];
}
}
// fibnaci way
class Solution {
public int climbStairs(int n) {
if(n <= 0) return 0;
if(n <= 2) return n;
int f = 1;
int s = 2;
int t = 0;
for(int i = 2; i < n; i++) {
t = f+s;
f = s;
s = t;
}
return t;
}
}