-
Notifications
You must be signed in to change notification settings - Fork 452
Expand file tree
/
Copy pathSolution3.java
More file actions
33 lines (24 loc) · 888 Bytes
/
Solution3.java
File metadata and controls
33 lines (24 loc) · 888 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
import java.util.Arrays;
// 动态规划
public class Solution3 {
public int fib(int n){
int[] memo = new int[n + 1];
Arrays.fill(memo, -1);
memo[0] = 0;
memo[1] = 1;
for(int i = 2 ; i <= n ; i ++)
memo[i] = memo[i - 1] + memo[i - 2];
return memo[n];
}
public static void main(String[] args) {
//int n = 42;
int n = 1000; // 注意: 我们使用n = 1000只是为了测试性能, 实际上会溢出
// 斐波那契额数列是以指数速度上涨的
Solution3 solution = new Solution3();
long startTime = System.currentTimeMillis();
int res = solution.fib(n);
long endTime = System.currentTimeMillis();
System.out.println("fib(" + n + ") = " + res);
System.out.println("time : " + (endTime - startTime) + " ms");
}
}