{"id":127268,"date":"2020-06-05T17:11:27","date_gmt":"2020-06-05T09:11:27","guid":{"rendered":"http:\/\/4563.org\/?p=127268"},"modified":"2020-06-05T17:11:27","modified_gmt":"2020-06-05T09:11:27","slug":"%e4%b8%80%e4%b8%aa%e5%a4%b1%e8%b4%a5%e7%9a%84-%e3%80%8cfibonacci-number%e3%80%8d%ef%bc%8c%e6%83%b3%e8%af%b7%e6%95%99%e4%b8%8b%e4%b8%ba%e4%bb%80%e4%b9%88%e4%bb%a3%e7%90%86%e6%b2%a1%e6%9c%89%e3%80%8c","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=127268","title":{"rendered":"\u4e00\u4e2a\u5931\u8d25\u7684 \u300cFibonacci Number\u300d\uff0c\u60f3\u8bf7\u6559\u4e0b\u4e3a\u4ec0\u4e48\u4ee3\u7406\u6ca1\u6709\u300c\u6b63\u786e\u4f7f\u7528\u300d\u9012\u5f52\u5185\u90e8\u7684\u7f13\u5b58\u7ed3\u679c\u3002"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u4e00\u4e2a\u5931\u8d25\u7684 \u300cFibonacci Number\u300d\uff0c\u60f3\u8bf7\u6559\u4e0b\u4e3a\u4ec0\u4e48\u4ee3\u7406\u6ca1\u6709\u300c\u6b63\u786e\u4f7f\u7528\u300d\u9012\u5f52\u5185\u90e8\u7684\u7f13\u5b58\u7ed3\u679c\u3002               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : JayLin1011 <\/span>  <span><i><\/i> 10<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<pre><code>const tailFibonacci = (n, y = 1, result = 1) =&gt;   n &lt;= 1 ? result : tailFibonacci(n - 1, result, y + result);  const proxyFibonacci = new Proxy(tailFibonacci, {   apply(target, context, [n, ...args]) {     !Reflect.has(target, 'cache') &amp;&amp; Reflect.set(target, 'cache', new Map());       const cache = Reflect.get(target, 'cache');      return cache.has(n)       ? cache.get(n)       : cache.set(n, Reflect.apply(target, context, [n, ...args])).get(n);   }, });  console.log(proxyFibonacci(1)); console.log(proxyFibonacci(2)); console.log(proxyFibonacci(3)); console.log(proxyFibonacci(4)); console.log(proxyFibonacci(5)); console.log(proxyFibonacci(5)); console.log(proxyFibonacci(5));  <\/code><\/pre>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>4<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"2074039\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : calmzhu <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5728\u4f60\u7684\u6bcf\u4e2a console.log \u4e0b\u9762\u52a0\u4e00\u884c\u8fd9\u4e2a<br \/>console.log(tailFibonacci[&#8216;cache&#8217;])<br \/>\u4f60\u5c31\u53d1\u73b0\u9012\u5f52\u94fe\u5b9e\u9645\u662f\u65ad\u7684\u3002<\/p>\n<p>\u770b\u5012\u6570\u7b2c\u4e09\u884c\u3002\u8c03\u7528\u7684\u662f Reflect.apply \u3002 \u4e5f\u5c31\u662f\u539f\u6765\u7684\u672a\u4ee3\u7406\u7684 tailFibonacci \u3002<br \/>\u4e5f\u5c31\u662f\u9664\u975e\u663e\u793a\u8dd1 1\uff0c2\uff0c3&#8230;.99 \u7684 proxyFibonacci(n)\u7684\u5168\u90e8\u3002<br \/>\u76f4\u63a5\u52a0\u4e00\u4e2a proxyFibonacci(100)\u7684\u5316.\u5728 100 \u91cc\u9762\u5c31\u76f4\u63a5\u8df3\u8f6c\u5230 tailFibonacci \uff08 100 \uff09\u4e0a\u53bb\u4e86\u3002\u6240\u4ee5\u9012\u5f52\u94fe\u8d70\u7684\u662f tailFibonacci \u800c\u975e\u671f\u671b\u7684 proxyFibonacci                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2074040\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : iintothewind <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6ca1\u4ed4\u7ec6\u770b\u4f60\u7684\u4ee3\u7801, \u9644\u4e0a\u4e00\u6bb5\u6211\u4e4b\u524d scala \u5199\u7684\u9012\u5f52\u7248:<br \/> def fibonacci(n: Int): Int = {<br \/> @tailrec<br \/> def fibTail(n: Int, a: Int, b: Int): Int = n match {<br \/> case 0 =&gt; a<br \/> case _ =&gt; fibTail(n &#8211; 1, b, a + b)<br \/> }<\/p>\n<p> fibTail(n, 0, 1)<br \/> }                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2074041\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : JayLin1011 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @calmzhu \u4f60\u8bf4\u7684\u5bf9\uff0c\u8fd9\u91cc\u53ea\u662f\u7f13\u5b58\u4e86\u90e8\u5206\u8ba1\u7b97\u7ed3\u679c\uff0c\u9012\u5f52\u5230\u4ee3\u7406\u3002<br \/>\u6211\u7684\u521d\u8877\u662f\u9012\u5f52\u7684\u65f6\u5019\u539f\u51fd\u6570\u5148\u5c1d\u8bd5\u83b7\u53d6\u7f13\u5b58\uff0c\u6ca1\u6709\u547d\u4e2d\u624d\u771f\u6b63\u6267\u884c\u8ba1\u7b97\uff0c\u6240\u4ee5\u81ea\u4ee5\u4e3a\u6bcf\u6b21\u9012\u5f52\u539f\u51fd\u6570\u4e5f\u4f1a\u7ecf\u8fc7\u4ee3\u7406\u3002<br \/>\u4f46\u6211\u5c1d\u8bd5\u9012\u5f52\u4ee3\u7406\u4e5f\u6ca1\u80fd\u771f\u6b63\u8bfb\u53d6\u5230\uff0c\u611f\u89c9\u4ee3\u7406\u4e0d\u9002\u5408\u8fd9\u4e2a\u573a\u666f\u3002<br \/>\u76ee\u524d\u6210\u529f\u7684\u65b9\u5f0f\u662f\u4e0d\u7528\u4ee3\u7406\uff0c\u5c06\u7f13\u5b58\u548c\u529f\u80fd\u5b9e\u73b0\u5728\u539f\u51fd\u6570\u5185\u90e8\u8026\u5408\u3002<br \/>&#8220;`js<br \/>let count = 0;<br \/>const fibonacci = (N, cache = new Map()) =&gt; {<br \/> cache.set(0, 1).set(1, 1);<\/p>\n<p> if (N &lt;= 1) {<br \/> return 1;<br \/> }<\/p>\n<p> const memorize = n =&gt; {<br \/> if (cache.has(n)) {<br \/> count++;<\/p>\n<p> return cache.get(n);<br \/> }<\/p>\n<p> return cache.set(n, memorize(n &#8211; 1) + memorize(n &#8211; 2)).get(n);<br \/> };<\/p>\n<p> return memorize(N);<br \/>};<\/p>\n<p>console.log(fibonacci(1));<br \/>console.log(fibonacci(2));<br \/>console.log(fibonacci(3));<br \/>console.log(fibonacci(4));<br \/>console.log(fibonacci(5));<\/p>\n<p>console.log(&#8216;\u7f13\u5b58\u547d\u4e2d\u6570\uff1a&#8217;, count);<\/p>\n<p>&#8220;`                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2074042\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : JayLin1011 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @iintothewind \u6ca1\u4ed4\u7ec6\u770b\u4f60\u7684\u4ee3\u7801,\u6ca1\u5b66\u8fc7 `scala` \u3002<br \/>\u5927\u81f4\u601d\u8def\u5c31\u662f\uff1a\u5c3e\u9012\u5f52 + \u7f13\u5b58\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u4e2a\u5931\u8d25\u7684 \u300cFibonacci &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/127268"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=127268"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/127268\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=127268"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=127268"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=127268"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}