zoukankan      html  css  js  c++  java
  • 另一种fib高效实现

    缓存形式的实现

    var memoize = function(fn) {
      var cache = [];
      return function(i) {
        return (i in cache) ? cache[i] :
          (cache[i] = fn.call(arguments.callee, i));
      };
    }
     
    var fib = new memoize(function(i) {
      if (i == 0 || i == 1)
        return 1;
      return this(i-1) + this(i-2);
    })
    
    
    

    新的实现:

    function fib(n) {
      function _fib(n, a, b, arg1, arg2) {
        if(n>1) _fib(n-1, a, b, arguments, arg1);
        return arg1[2] = arg2[1] = a+b;
      }
    
      return _fib(n, 0, 1, [], []);
    }
    

  • 相关阅读:
    Django form
    centos 配置yum 源
    VMware clone centos 没有获取到ip
    python 自定义分页
    模态对话框
    Keepalived HAProxy mysql 配置HA
    HAProxy + mysql 配置
    mysql 配置主从
    关于python很好的网站和书籍
    【文件系统】dumpe2fs命令
  • 原文地址:https://www.cnblogs.com/rubylouvre/p/1830692.html
Copyright © 2011-2022 走看看