首页 > 代码库 > 【基础算法】- 个人认为最快的 Fibonacci 程序

【基础算法】- 个人认为最快的 Fibonacci 程序

?
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
public class Fibonacci {
     
    private static Map<Long,Long> map = new HashMap<Long,Long>();
    static{
        map.put(0L, 1L);
        map.put(1L, 1L);
    }
     
    public static void main(String[] args) {
         
        long t = System.currentTimeMillis();
        Fibonacci fi = new Fibonacci();
        System.out.println(fi.cal(36L,map));
        System.out.println(System.currentTimeMillis() - t);
        System.out.println();
        t = System.currentTimeMillis();
        fi = new Fibonacci();
        System.out.println(fi.cal(36L));
        System.out.println(System.currentTimeMillis() - t);
    }
     
    public Long cal(Long n, Map<Long,Long> map){
         
        if(map.get(n) != null){
            return map.get(n);
        }else{
            long i = cal(n-1,map) + cal(n-2,map);
            map.put(n, i);
            return i;
        }
    }