-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathLeetCode_70_18.java
More file actions
61 lines (50 loc) · 1.27 KB
/
LeetCode_70_18.java
File metadata and controls
61 lines (50 loc) · 1.27 KB
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
package Week_04.id_18;
/**
* @author LiveForExperience
* @date 2019/6/27 17:48
*/
public class LeetCode_70_18 {
public int climbStairs(int n) {
return climbStairs(n, new int[n + 1]);
}
private int climbStairs(int n, int[] nums) {
if (n <= 2) {
return n;
}
if (nums[n] > 0) {
return nums[n];
}
int num = climbStairs(n - 1, nums) + climbStairs(n - 2, nums);
nums[n] = num;
return num;
}
public int climbStairs1(int n) {
if (n <= 2) {
return n;
}
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];
}
public int climbStairs2(int n) {
if (n <= 2) {
return n;
}
int first = 1, second = 2;
for (int i = 3; i <= n; i++) {
int third = first + second;
first = second;
second = third;
}
return second;
}
public int climbStairs3(int n) {
double sqrt5 = Math.sqrt(5);
double fibn = Math.pow((1 + sqrt5) / 2, n + 1) - Math.pow((1 - sqrt5) / 2, n + 1);
return (int) (fibn / sqrt5);
}
}