-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode0070.java
More file actions
40 lines (37 loc) · 949 Bytes
/
Copy pathLeetCode0070.java
File metadata and controls
40 lines (37 loc) · 949 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
34
35
36
37
38
39
40
/* Climbing Stairs
* Input: 2
* Output: 2
* Explanation: There are two ways to climb to the top.
1. 1 step + 1 step
2. 2 steps
* */
public class LeetCode0070 {
public static void main(String args[]) {
int n = 45;
System.out.println(climbStairs(n));
System.out.println(climbStairs1(n));
}
public static int climbStairs(int n) {
if (n == 0)
return 0;
else if (n == 1)
return 1;
else if (n == 2)
return 2;
else
return climbStairs(n - 1) + climbStairs(n - 2);
}
//Fibonacci Number
public static int climbStairs1(int n) {
if (n == 1) {
return 1;
}
int[] dp = new int[n + 1];
dp[1] = 1;
dp[2] = 2;
for (int i = 3; i <= n; i++) {
dp[i] = dp[i - 1] + dp[i - 2];
}
return dp[n];
}
}