zoukankan      html  css  js  c++  java
  • memorize

     1 function memorize(f){
     2                 var cache = {};
     3                 return function(){
     4                     var key = arguments.length + Array.prototype.join.apply(this,arguments);
     5                     if(key in cache){
     6 
     7                         return cache[key];
     8                     } else{
     9                         return cache[key] = f.apply(this, arguments);
    10                     }
    11                 }
    12             }
    13             function add(a,b){
    14                 return a + b;
    15             }
    16             var adder = memorize(add);
    17             for(var i=0;i<4;i++){
    18                 console.log(adder(1,2));
    19             }
  • 相关阅读:
    Vijos1986
    vijos1790
    洛谷1005
    洛谷3381
    bzoj4034
    bzoj1564
    bzoj2648
    洛谷3348
    jzoi4964
    codevs3990
  • 原文地址:https://www.cnblogs.com/fanhc/p/3255048.html
Copyright © 2011-2022 走看看