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

时间:2014-06-03 07:24:25   收藏:0   阅读:221
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;
        }
    }

 

【基础算法】- 个人认为最快的 Fibonacci 程序,布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!