-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClimbingStairs.java
More file actions
50 lines (46 loc) · 1.05 KB
/
ClimbingStairs.java
File metadata and controls
50 lines (46 loc) · 1.05 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
package com.leetcode;
/**
* Created by jamylu on 2018/1/19.
* leetcode070
* 爬台阶
*/
public class ClimbingStairs {
// 使用递归超时
public int climbStairs(int n) {
if (n == 1) {
return 1;
} else if (n == 2) {
return 2;
} else {
return climbStairs(n - 1) + climbStairs(n - 2);
}
}
// 动态规划,申请辅助空间O(n)
public int stairs(int n) {
int[] dp = new int[n + 2];
dp[1] = 1;
dp[2] = 2;
for (int i = 3; i <= n; i++) {
dp[i] = dp[i - 1] + dp[i - 2];
}
return dp[n];
}
// Fibonacci数 O(1)辅助空间
public int Fibonacci(int n) {
if (n == 1) {
return 1;
}
if (n == 2) {
return 2;
}
int first = 1;
int second = 2;
int current = 0;
for (int i = 3; i <= n; i++) {
current = first + second;
first = second;
second = current;
}
return current;
}
}