{"id":99629,"date":"2026-09-02T23:56:46","date_gmt":"2026-09-02T15:56:46","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/99629.html"},"modified":"2026-09-02T23:56:46","modified_gmt":"2026-09-02T15:56:46","slug":"%e4%b8%80%e5%a4%a9%e4%b8%80%e9%81%93%e7%ae%97%e6%b3%95%e9%a2%98%ef%bc%8824%ef%bc%89%ef%bc%9alru%e7%bc%93%e5%ad%98%e6%9c%ba%e5%88%b6","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/99629.html","title":{"rendered":"\u4e00\u5929\u4e00\u9053\u7b97\u6cd5\u9898\uff0824\uff09\uff1aLRU\u7f13\u5b58\u673a\u5236"},"content":{"rendered":"<h3>146. LRU \u7f13\u5b58<\/h3>\n<\/p>\n<h4>\u6587\u7ae0\u76ee\u5f55<\/h4>\n<ul>\n<li>\n<ul>\n<li>[146. LRU \u7f13\u5b58](https:\/\/leetcode.cn\/problems\/lru-cache\/)<\/li>\n<li>\u601d\u8def<\/li>\n<li>\n<ul>\n<li>Java\u4ee3\u7801<\/li>\n<li>Python\u4ee3\u7801<\/li>\n<li>Golang\u4ee3\u7801<\/li>\n<li>\u6ce8\u610f<\/li>\n<\/ul>\n<\/li>\n<li>\u7ed3\u8bed<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>\u8bf7\u4f60\u8bbe\u8ba1\u5e76\u5b9e\u73b0\u4e00\u4e2a\u6ee1\u8db3 LRU (\u6700\u8fd1\u6700\u5c11\u4f7f\u7528) \u7f13\u5b58 \u7ea6\u675f\u7684\u6570\u636e\u7ed3\u6784\u3002<\/p>\n<p>\u5b9e\u73b0 LRUCache \u7c7b&#xff1a;<\/p>\n<ul>\n<li>LRUCache(int capacity) \u4ee5 \u6b63\u6574\u6570 \u4f5c\u4e3a\u5bb9\u91cf capacity \u521d\u59cb\u5316 LRU \u7f13\u5b58<\/li>\n<li>int get(int key) \u5982\u679c\u5173\u952e\u5b57 key \u5b58\u5728\u4e8e\u7f13\u5b58\u4e2d&#xff0c;\u5219\u8fd4\u56de\u5173\u952e\u5b57\u7684\u503c&#xff0c;\u5426\u5219\u8fd4\u56de -1 \u3002<\/li>\n<li>void put(int key, int value) \u5982\u679c\u5173\u952e\u5b57 key \u5df2\u7ecf\u5b58\u5728&#xff0c;\u5219\u53d8\u66f4\u5176\u6570\u636e\u503c value &#xff1b;\u5982\u679c\u4e0d\u5b58\u5728&#xff0c;\u5219\u5411\u7f13\u5b58\u4e2d\u63d2\u5165\u8be5\u7ec4 key-value \u3002\u5982\u679c\u63d2\u5165\u64cd\u4f5c\u5bfc\u81f4\u5173\u952e\u5b57\u6570\u91cf\u8d85\u8fc7 capacity &#xff0c;\u5219\u5e94\u8be5 \u9010\u51fa \u6700\u4e45\u672a\u4f7f\u7528\u7684\u5173\u952e\u5b57\u3002<\/li>\n<\/ul>\n<p>\u51fd\u6570 get \u548c put \u5fc5\u987b\u4ee5 O(1) \u7684\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u8fd0\u884c\u3002<\/p>\n<p>\u793a\u4f8b&#xff1a;<\/p>\n<p>\u8f93\u5165<br \/>\n[&#034;LRUCache&#034;, &#034;put&#034;, &#034;put&#034;, &#034;get&#034;, &#034;put&#034;, &#034;get&#034;, &#034;put&#034;, &#034;get&#034;, &#034;get&#034;, &#034;get&#034;]<br \/>\n[[2], [1, 1], [2, 2], [1], [3, 3], [2], [4, 4], [1], [3], [4]]<br \/>\n\u8f93\u51fa<br \/>\n[null, null, null, 1, null, -1, null, -1, 3, 4]<\/p>\n<p>\u89e3\u91ca<br \/>\nLRUCache lRUCache &#061; new LRUCache(2);<br \/>\nlRUCache.put(1, 1); \/\/ \u7f13\u5b58\u662f {1&#061;1}<br \/>\nlRUCache.put(2, 2); \/\/ \u7f13\u5b58\u662f {1&#061;1, 2&#061;2}<br \/>\nlRUCache.get(1);    \/\/ \u8fd4\u56de 1<br \/>\nlRUCache.put(3, 3); \/\/ \u8be5\u64cd\u4f5c\u4f1a\u4f7f\u5f97\u5173\u952e\u5b57 2 \u4f5c\u5e9f&#xff0c;\u7f13\u5b58\u662f {1&#061;1, 3&#061;3}<br \/>\nlRUCache.get(2);    \/\/ \u8fd4\u56de -1 (\u672a\u627e\u5230)<br \/>\nlRUCache.put(4, 4); \/\/ \u8be5\u64cd\u4f5c\u4f1a\u4f7f\u5f97\u5173\u952e\u5b57 1 \u4f5c\u5e9f&#xff0c;\u7f13\u5b58\u662f {4&#061;4, 3&#061;3}<br \/>\nlRUCache.get(1);    \/\/ \u8fd4\u56de -1 (\u672a\u627e\u5230)<br \/>\nlRUCache.get(3);    \/\/ \u8fd4\u56de 3<br \/>\nlRUCache.get(4);    \/\/ \u8fd4\u56de 4<\/p>\n<h3>\u601d\u8def<\/h3>\n<ul>\n<li>\n<p>\u4e09\u79cd\u8bed\u8a00\u5b9e\u73b0\u4e00\u4e0b\u8fd9\u9053\u9898\u76ee&#xff0c;\u5206\u522b\u662fJava&#xff0c;Python&#xff0c;Golang\u3002\u6211\u5c06\u5e26\u7740\u4f60\u4e00\u6b65\u4e00\u6b65\u601d\u8003&#xff0c;\u6309\u7167\u7b2c\u4e00\u6b21\u89c1\u5230\u8fd9\u4e2a\u9898\u76ee\u7684\u65b9\u5f0f\u5c42\u5c42\u9012\u8fdb\u5730\u89e3\u6790\u8fd9\u9053\u9898\u76ee\u7684\u7b97\u6cd5\u5b9e\u73b0&#xff0c;\u4e00\u8fb9\u601d\u8003\u4e00\u8fb9\u770b&#xff0c;\u6536\u83b7\u975e\u51e1\u3002<\/p>\n<\/li>\n<li>\n<p>\u9996\u5148\u6211\u4eec\u8981\u7406\u6e05\u695a\u8fd9\u9053\u9898\u76ee\u5728\u8bbe\u8ba1\u7684\u65f6\u5019\u6211\u4eec\u9700\u8981\u77e5\u9053\u54ea\u4e9b\u91cf\u3002\u6211\u4eec\u8981\u5b58\u4e00\u4e2a\u952e\u503c\u5bf9&#xff0c;\u540c\u65f6\u8981\u7ba1\u7406\u6bcf\u4e00\u5bf9\u952e\u503c\u5bf9\u7684\u4f18\u5148\u7ea7&#xff0c;\u5f53\u7f13\u5b58\u6ee1\u4e86\u7684\u65f6\u5019&#xff0c;\u628a\u6700\u4e45\u672a\u4f7f\u7528\u7684\u952e\u503c\u5bf9\u5220\u9664\u3002\u9898\u76ee\u8fd8\u8981\u6c42put\u548cget\u5fc5\u987b\u8fbe\u5230O(1)\u7ea7\u522b\u7684\u65f6\u95f4\u590d\u6742\u5ea6&#xff0c;\u96be\u5ea6\u8fd8\u662f\u5f88\u5927\u7684\u3002<\/p>\n<\/li>\n<li>\n<p>\u6211\u4eec\u5148\u4eceO(1)\u67e5\u627e\u8fd9\u6761\u601d\u8def\u51fa\u53d1\u3002O(1)\u67e5\u627e\u53ea\u6709\u4e24\u4e2a\u6570\u636e\u7ed3\u6784\u53ef\u4ee5\u6ee1\u8db3&#xff0c;\u5c31\u662f\u6570\u7ec4\u548c\u54c8\u5e0c\u8868\u3002\u4f46\u662f\u6570\u7ec4\u7684\u67e5\u627e\u662f\u901a\u8fc7\u4e0b\u6807\u7d22\u5f15\u67e5\u627e&#xff0c;\u5982\u679c\u6211\u4eec\u8981\u7528\u6570\u7ec4\u5b58\u50a8key-value\u5bf9&#xff0c;key\u672c\u8eab\u5fc5\u987b\u662f\u6574\u6570&#xff0c;\u800c\u4e14\u6700\u597d\u8fd8\u662f\u8fde\u7eed\u7d27\u51d1\u7684\u6574\u6570&#xff0c;\u5426\u5219\u5c31\u4f1a\u6d6a\u8d39\u5927\u91cf\u7a7a\u95f4\u3002\u66f4\u9ebb\u70e6\u7684\u662f&#xff0c;\u6211\u4eec\u7684key\u4e0d\u4e00\u5b9a\u90fd\u662f\u6574\u6570&#xff08;\u6bd4\u5982\u53ef\u80fd\u662f\u5b57\u7b26\u4e32&#xff09;&#xff0c;\u5373\u4f7f\u90fd\u662f\u6574\u6570&#xff0c;\u7528\u6570\u7ec4\u5b58\u4e5f\u4f1a\u4ea7\u751f\u5de8\u5927\u7684\u5185\u5b58\u7a7a\u6d1e\u3002\u90a3\u80fd\u4e0d\u80fd\u7528\u6570\u7ec4\u5b58value&#xff0c;\u7136\u540e\u53e6\u5916\u5efa\u7acb\u4e00\u4e2a\u6620\u5c04\u5173\u7cfb\u628akey\u8f6c\u6362\u6210\u6570\u7ec4\u4e0b\u6807\u5462&#xff1f;\u5f53\u7136\u53ef\u4ee5&#xff0c;\u8fd9\u5c31\u662f\u54c8\u5e0c\u8868\u505a\u7684\u4e8b\u60c5\u3002\u4f46\u6211\u4eec\u5982\u679c\u5148\u8d70\u8fd9\u6761\u8def&#xff0c;\u5c31\u7b49\u4e8e\u7ed5\u4e86\u4e00\u5708\u6700\u540e\u8fd8\u662f\u56de\u5230\u54c8\u5e0c\u8868&#xff0c;\u53cd\u800c\u591a\u7ef4\u62a4\u4e86\u4e00\u4e2a\u6620\u5c04\u5c42&#xff0c;\u5185\u5b58\u5f00\u9500\u66f4\u5927\u3002<\/p>\n<\/li>\n<li>\n<p>\u6240\u4ee5\u6211\u4eec\u76f4\u63a5\u4f7f\u7528\u54c8\u5e0c\u8868\u6765\u4f5c\u4e3a\u5b58\u50a8key-value\u7684\u6570\u636e\u7ed3\u6784&#xff0c;\u4e00\u6b65\u5230\u4f4d\u3002\u8fd9\u91cc\u6211\u4eec\u5c31\u5b9e\u73b0\u4e86get\u7684O(1)\u7ea7\u522b\u67e5\u627e\u3002\u4f46\u6ce8\u610f&#xff0c;get\u7684O(1)\u641e\u5b9a\u4e86&#xff0c;\u4e3a\u4ec0\u4e48\u6ca1\u6709\u8bf4\u540c\u65f6\u5b9e\u73b0\u4e86put\u7684O(1)\u5462&#xff1f; \u56e0\u4e3a\u6211\u4eec\u8fd8\u7f3a\u5c11\u4e00\u4e2a\u5173\u952e\u673a\u5236&#xff1a;\u4f18\u5148\u7ea7\u7ba1\u7406\u3002<\/p>\n<\/li>\n<li>\n<p>\u6211\u4eec\u5f15\u5165\u54c8\u5e0c\u8868\u53ea\u662f\u89e3\u51b3\u4e86\u5b58\u50a8\u548c\u67e5\u627e\u7684\u95ee\u9898&#xff0c;\u4f46\u54c8\u5e0c\u8868\u672c\u8eab\u4e0d\u8bb0\u5f55\u987a\u5e8f\u3002 \u6240\u4ee5\u6211\u4eec\u8981\u60f3\u4e00\u4e2a\u529e\u6cd5\u6765\u8bb0\u5f55\u6bcf\u4e00\u4e2a\u952e\u503c\u5bf9\u7684\u4f18\u5148\u7ea7\u3002\u4e00\u5f00\u59cb\u6211\u60f3\u628a\u54c8\u5e0c\u8868\u7684&#034;\u503c&#034;\u8bbe\u7f6e\u4e3a\u4e00\u4e2a&#034;\u7ed3\u6784\u4f53&#034;&#xff08;\u6216\u7c7b&#xff09;&#xff0c;\u5728\u8fd9\u4e2a\u7ed3\u6784\u4f53\u91cc\u9762\u5b58\u4e24\u4e2a\u503c&#xff1a;\u4e00\u4e2a\u662f\u539f\u672c\u7684value&#xff0c;\u53e6\u4e00\u4e2a\u662f\u8bb0\u5f55\u4f18\u5148\u7ea7\u7684\u53d8\u91cf\u3002\u6bd4\u5982\u53ef\u4ee5\u7528\u65f6\u95f4\u6233&#xff0c;\u4f46\u6bcf\u6b21\u83b7\u53d6\u672c\u5730\u65f6\u95f4\u5728\u7b97\u6cd5\u9898\u91cc\u4e0d\u592a\u5408\u9002&#xff1b;\u4e5f\u53ef\u4ee5\u7528\u8ba1\u6570\u5668\u6a21\u62df\u65f6\u95f4&#xff0c;\u6bcf\u6b21get\u6216put\u90fd\u8ba9\u8ba1\u6570\u5668\u52a0\u4e00&#xff0c;\u5728\u66f4\u65b0\u6216\u521b\u5efa\u952e\u503c\u5bf9\u65f6\u628a\u5f53\u524d\u8ba1\u6570\u5b58\u8fdb\u53bb\u3002\u867d\u7136\u7406\u8bba\u4e0a\u53ef\u884c&#xff0c;\u4f46\u8fd9\u6837\u7684\u4ee3\u4ef7\u662f&#xff1a;\u5f53\u7f13\u5b58\u6ee1\u4e86\u9700\u8981\u5220\u9664\u6700\u65e7\u5143\u7d20\u65f6&#xff0c;\u6211\u4eec\u5fc5\u987bO(n)\u904d\u5386\u6240\u6709\u952e\u503c\u5bf9\u624d\u80fd\u627e\u5230\u8ba1\u6570\u5668\u6700\u5c0f\u7684\u90a3\u4e2a&#xff0c;\u8fd9\u4e0d\u7b26\u5408O(1)\u7684\u8981\u6c42\u3002<\/p>\n<\/li>\n<li>\n<p>\u90a3\u6211\u4eec\u6362\u4e00\u79cd\u601d\u8def\u3002 \u5982\u679c\u4e0d\u7528\u8ba1\u6570\u5668&#xff0c;\u80fd\u4e0d\u80fd\u6309\u7167\u8bbf\u95ee\u987a\u5e8f\u5929\u7136\u5730\u6392\u5217\u952e\u503c\u5bf9\u5462&#xff1f;\u8fd9\u5176\u5b9e\u5c31\u662f\u94fe\u8868\u8981\u505a\u7684\u4e8b\u60c5\u3002\u5982\u679c\u7528\u5934\u63d2\u6cd5&#xff0c;\u6bcf\u6b21\u65b0\u8282\u70b9\u6216\u521a\u8bbf\u95ee\u8fc7\u7684\u8282\u70b9\u90fd\u63d2\u5165\u5230head\u540e\u9762&#xff0c;\u90a3\u4e48\u8d8a\u9760\u8fd1head\u7684\u8282\u70b9\u5c31\u662f\u8d8a\u65b0\u7684&#xff0c;\u8d8a\u9760\u8fd1tail\u7684\u8282\u70b9\u5c31\u662f\u8d8a\u65e7\u7684&#xff0c;\u4f18\u5148\u7ea7\u987a\u5e8f\u5c31\u8fd9\u6837\u88ab\u94fe\u8868\u5929\u7136\u7ef4\u62a4\u4e86\u3002\u5f53\u94fe\u8868\u957f\u5ea6\u8fbe\u5230\u7f13\u5b58\u5bb9\u91cf\u65f6&#xff0c;\u6211\u4eec\u76f4\u63a5\u5220\u9664tail\u524d\u9762\u7684\u8282\u70b9&#xff08;\u5373\u6700\u4e45\u672a\u4f7f\u7528\u7684\u8282\u70b9&#xff09;&#xff0c;\u7136\u540e\u65b0\u8282\u70b9\u5934\u63d2\u5373\u53ef\u3002\u8fd9\u91cc\u6709\u4e00\u4e2a\u5173\u952e\u70b9&#xff1a;\u5355\u94fe\u8868\u4e2d\u5220\u9664\u4efb\u610f\u8282\u70b9\u9700\u8981O(n)\u65f6\u95f4&#xff0c;\u6240\u4ee5\u6211\u4eec\u7528\u53cc\u5411\u94fe\u8868&#xff0c;\u8fd9\u6837\u5220\u9664\u64cd\u4f5c\u5c31\u662fO(1)\u3002<\/p>\n<\/li>\n<li>\n<p>\u6b64\u65f6\u5b58\u50a8\u6570\u636e\u7684\u4e0d\u518d\u662f\u72ec\u7acb\u7684\u53d8\u91cf\u6216\u7ed3\u6784\u4f53&#xff0c;\u800c\u662f\u94fe\u8868\u7684\u8282\u70b9\u3002 \u6211\u4eec\u628a\u54c8\u5e0c\u8868\u7684&#034;\u503c&#034;\u8bbe\u7f6e\u4e3a\u94fe\u8868\u7684\u6307\u9488\u3002\u8fd9\u6837\u5c31\u5b8c\u7f8e\u7ed3\u5408\u4e86\u54c8\u5e0c\u8868\u548c\u53cc\u5411\u94fe\u8868\u7684\u4f18\u52bf&#xff1a;\u901a\u8fc7\u54c8\u5e0c\u8868\u5b9e\u73b0O(1)\u67e5\u627e&#xff0c;\u901a\u8fc7\u94fe\u8868\u7ef4\u62a4\u4f18\u5148\u7ea7\u987a\u5e8f\u3002\u5f53\u6211\u4eec\u8c03\u7528get\u65f6&#xff0c;\u5148\u5728\u54c8\u5e0c\u8868\u4e2d\u67e5\u627ekey&#xff0c;\u5982\u679c\u4e0d\u5b58\u5728\u8fd4\u56de-1&#xff1b;\u5982\u679c\u5b58\u5728&#xff0c;\u76f4\u63a5\u901a\u8fc7\u6307\u9488\u627e\u5230\u94fe\u8868\u8282\u70b9&#xff0c;\u8bbf\u95eevalue&#xff0c;\u540c\u65f6\u628a\u8be5\u8282\u70b9\u79fb\u5230head\u540e\u9762\u66f4\u65b0\u4f18\u5148\u7ea7&#xff08;\u56e0\u4e3a\u5b83\u521a\u521a\u88ab\u8bbf\u95ee\u4e86&#xff09;\u3002\u5f53\u6211\u4eec\u8c03\u7528put\u65f6&#xff0c;\u5982\u679ckey\u5df2\u5b58\u5728&#xff0c;\u5c31\u4fee\u6539value&#xff0c;\u7136\u540e\u628a\u8be5\u8282\u70b9\u79fb\u5230head\u540e\u9762&#xff1b;\u5982\u679ckey\u4e0d\u5b58\u5728&#xff0c;\u5c31\u521b\u5efa\u65b0\u8282\u70b9&#xff0c;\u5934\u63d2\u5230head\u540e\u9762&#xff0c;\u540c\u65f6\u5b58\u5165\u54c8\u5e0c\u8868\u3002\u6bcf\u6b21\u63d2\u5165\u540e\u68c0\u67e5\u662f\u5426\u8d85\u8fc7\u5bb9\u91cf&#xff0c;\u5982\u679c\u8d85\u8fc7\u5c31\u5220\u9664tail\u524d\u9762\u7684\u8282\u70b9&#xff0c;\u5e76\u4ece\u54c8\u5e0c\u8868\u4e2d\u79fb\u9664\u5bf9\u5e94\u7684key\u3002\u8fd9\u6837\u5c31\u5b9e\u73b0\u4e86O(1)\u65f6\u95f4\u590d\u6742\u5ea6\u7684put\u548cget\u64cd\u4f5c\u3002\u7a7a\u95f4\u590d\u6742\u5ea6\u65b9\u9762&#xff0c;\u54c8\u5e0c\u8868\u548c\u94fe\u8868\u5404\u5b58\u4e00\u4efd\u6570\u636e&#xff0c;\u7269\u7406\u4e0a\u662fO(2n)&#xff0c;\u4f46\u5927O\u8bb0\u6cd5\u5ffd\u7565\u5e38\u6570\u7cfb\u6570&#xff0c;\u8bb0\u4e3aO(n)\u3002<\/p>\n<\/li>\n<li>\n<p>\u4ee3\u7801\u5c55\u793a<\/p>\n<\/li>\n<\/ul>\n<h4>Java\u4ee3\u7801<\/h4>\n<ul>\n<li> <span class=\"token keyword\">class<\/span> <span class=\"token class-name\">LRUCache<\/span> <span class=\"token keyword\">extends<\/span> <span class=\"token class-name\">LinkedHashMap<\/span><span class=\"token generics\"><span class=\"token punctuation\">&lt;<\/span><span class=\"token class-name\">Integer<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token class-name\">Integer<\/span><span class=\"token punctuation\">&gt;<\/span><\/span><span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token keyword\">private<\/span> <span class=\"token keyword\">int<\/span> capacity<span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token keyword\">public<\/span> <span class=\"token class-name\">LRUCache<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">int<\/span> capacity<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token keyword\">super<\/span><span class=\"token punctuation\">(<\/span>capacity<span class=\"token punctuation\">,<\/span> <span class=\"token number\">0.75F<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token boolean\">true<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token keyword\">this<\/span><span class=\"token punctuation\">.<\/span>capacity <span class=\"token operator\">&#061;<\/span> capacity<span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><\/p>\n<p>    <span class=\"token keyword\">public<\/span> <span class=\"token keyword\">int<\/span> <span class=\"token function\">get<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">int<\/span> key<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token keyword\">return<\/span> <span class=\"token keyword\">super<\/span><span class=\"token punctuation\">.<\/span><span class=\"token function\">getOrDefault<\/span><span class=\"token punctuation\">(<\/span>key<span class=\"token punctuation\">,<\/span> <span class=\"token operator\">&#8211;<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><\/p>\n<p>    <span class=\"token keyword\">public<\/span> <span class=\"token keyword\">void<\/span> <span class=\"token function\">put<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">int<\/span> key<span class=\"token punctuation\">,<\/span> <span class=\"token keyword\">int<\/span> value<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token keyword\">super<\/span><span class=\"token punctuation\">.<\/span><span class=\"token function\">put<\/span><span class=\"token punctuation\">(<\/span>key<span class=\"token punctuation\">,<\/span> value<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><\/p>\n<p>    <span class=\"token annotation punctuation\">&#064;Override<\/span><br \/>\n    <span class=\"token keyword\">protected<\/span> <span class=\"token keyword\">boolean<\/span> <span class=\"token function\">removeEldestEntry<\/span><span class=\"token punctuation\">(<\/span><span class=\"token class-name\">Map<span class=\"token punctuation\">.<\/span>Entry<\/span><span class=\"token generics\"><span class=\"token punctuation\">&lt;<\/span><span class=\"token class-name\">Integer<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token class-name\">Integer<\/span><span class=\"token punctuation\">&gt;<\/span><\/span> eldest<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token keyword\">return<\/span> <span class=\"token function\">size<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&gt;<\/span> capacity<span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p>\u4f5c\u8005&#xff1a;\u529b\u6263\u5b98\u65b9\u9898\u89e3<br \/>\n\u94fe\u63a5&#xff1a;https<span class=\"token operator\">:<\/span><span class=\"token operator\">\/<\/span><span class=\"token operator\">\/<\/span>leetcode<span class=\"token punctuation\">.<\/span>cn<span class=\"token operator\">\/<\/span>problems<span class=\"token operator\">\/<\/span>lru<span class=\"token operator\">&#8211;<\/span>cache<span class=\"token operator\">\/<\/span>solutions<span class=\"token operator\">\/<\/span><span class=\"token number\">259678<\/span><span class=\"token operator\">\/<\/span>lruhuan<span class=\"token operator\">&#8211;<\/span>cun<span class=\"token operator\">&#8211;<\/span>ji<span class=\"token operator\">&#8211;<\/span>zhi<span class=\"token operator\">&#8211;<\/span>by<span class=\"token operator\">&#8211;<\/span>leetcode<span class=\"token operator\">&#8211;<\/span>solution<span class=\"token operator\">\/<\/span><br \/>\n\u6765\u6e90&#xff1a;\u529b\u6263&#xff08;<span class=\"token class-name\">LeetCode<\/span>&#xff09;<br \/>\n\u8457\u4f5c\u6743\u5f52\u4f5c\u8005\u6240\u6709\u3002\u5546\u4e1a\u8f6c\u8f7d\u8bf7\u8054\u7cfb\u4f5c\u8005\u83b7\u5f97\u6388\u6743&#xff0c;\u975e\u5546\u4e1a\u8f6c\u8f7d\u8bf7\u6ce8\u660e\u51fa\u5904\u3002\n <\/li>\n<\/ul>\n<h4>Python\u4ee3\u7801<\/h4>\n<ul>\n<li> <span class=\"token keyword\">class<\/span> <span class=\"token class-name\">LRUCache<\/span><span class=\"token punctuation\">(<\/span>collections<span class=\"token punctuation\">.<\/span>OrderedDict<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span>\n<p>    <span class=\"token keyword\">def<\/span> <span class=\"token function\">__init__<\/span><span class=\"token punctuation\">(<\/span>self<span class=\"token punctuation\">,<\/span> capacity<span class=\"token punctuation\">:<\/span> <span class=\"token builtin\">int<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n        <span class=\"token builtin\">super<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">.<\/span>__init__<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><br \/>\n        self<span class=\"token punctuation\">.<\/span>capacity <span class=\"token operator\">&#061;<\/span> capacity<\/p>\n<p>    <span class=\"token keyword\">def<\/span> <span class=\"token function\">get<\/span><span class=\"token punctuation\">(<\/span>self<span class=\"token punctuation\">,<\/span> key<span class=\"token punctuation\">:<\/span> <span class=\"token builtin\">int<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&#8211;<\/span><span class=\"token operator\">&gt;<\/span> <span class=\"token builtin\">int<\/span><span class=\"token punctuation\">:<\/span><br \/>\n        <span class=\"token keyword\">if<\/span> key <span class=\"token keyword\">not<\/span> <span class=\"token keyword\">in<\/span> self<span class=\"token punctuation\">:<\/span><br \/>\n            <span class=\"token keyword\">return<\/span> <span class=\"token operator\">&#8211;<\/span><span class=\"token number\">1<\/span><br \/>\n        self<span class=\"token punctuation\">.<\/span>move_to_end<span class=\"token punctuation\">(<\/span>key<span class=\"token punctuation\">)<\/span><br \/>\n        <span class=\"token keyword\">return<\/span> self<span class=\"token punctuation\">[<\/span>key<span class=\"token punctuation\">]<\/span><\/p>\n<p>    <span class=\"token keyword\">def<\/span> <span class=\"token function\">put<\/span><span class=\"token punctuation\">(<\/span>self<span class=\"token punctuation\">,<\/span> key<span class=\"token punctuation\">:<\/span> <span class=\"token builtin\">int<\/span><span class=\"token punctuation\">,<\/span> value<span class=\"token punctuation\">:<\/span> <span class=\"token builtin\">int<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&#8211;<\/span><span class=\"token operator\">&gt;<\/span> <span class=\"token boolean\">None<\/span><span class=\"token punctuation\">:<\/span><br \/>\n        <span class=\"token keyword\">if<\/span> key <span class=\"token keyword\">in<\/span> self<span class=\"token punctuation\">:<\/span><br \/>\n            self<span class=\"token punctuation\">.<\/span>move_to_end<span class=\"token punctuation\">(<\/span>key<span class=\"token punctuation\">)<\/span><br \/>\n        self<span class=\"token punctuation\">[<\/span>key<span class=\"token punctuation\">]<\/span> <span class=\"token operator\">&#061;<\/span> value<br \/>\n        <span class=\"token keyword\">if<\/span> <span class=\"token builtin\">len<\/span><span class=\"token punctuation\">(<\/span>self<span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&gt;<\/span> self<span class=\"token punctuation\">.<\/span>capacity<span class=\"token punctuation\">:<\/span><br \/>\n            self<span class=\"token punctuation\">.<\/span>popitem<span class=\"token punctuation\">(<\/span>last<span class=\"token operator\">&#061;<\/span><span class=\"token boolean\">False<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p>\u4f5c\u8005&#xff1a;\u529b\u6263\u5b98\u65b9\u9898\u89e3<br \/>\n\u94fe\u63a5&#xff1a;https<span class=\"token punctuation\">:<\/span><span class=\"token operator\">\/\/<\/span>leetcode<span class=\"token punctuation\">.<\/span>cn<span class=\"token operator\">\/<\/span>problems<span class=\"token operator\">\/<\/span>lru<span class=\"token operator\">&#8211;<\/span>cache<span class=\"token operator\">\/<\/span>solutions<span class=\"token operator\">\/<\/span><span class=\"token number\">259678<\/span><span class=\"token operator\">\/<\/span>lruhuan<span class=\"token operator\">&#8211;<\/span>cun<span class=\"token operator\">&#8211;<\/span>ji<span class=\"token operator\">&#8211;<\/span>zhi<span class=\"token operator\">&#8211;<\/span>by<span class=\"token operator\">&#8211;<\/span>leetcode<span class=\"token operator\">&#8211;<\/span>solution<span class=\"token operator\">\/<\/span><br \/>\n\u6765\u6e90&#xff1a;\u529b\u6263&#xff08;LeetCode&#xff09;<br \/>\n\u8457\u4f5c\u6743\u5f52\u4f5c\u8005\u6240\u6709\u3002\u5546\u4e1a\u8f6c\u8f7d\u8bf7\u8054\u7cfb\u4f5c\u8005\u83b7\u5f97\u6388\u6743&#xff0c;\u975e\u5546\u4e1a\u8f6c\u8f7d\u8bf7\u6ce8\u660e\u51fa\u5904\u3002\n <\/li>\n<\/ul>\n<h4>Golang\u4ee3\u7801<\/h4>\n<ul>\n<li> <span class=\"token keyword\">type<\/span> Node <span class=\"token keyword\">struct<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    key  <span class=\"token builtin\">int<\/span><br \/>\n    val  <span class=\"token builtin\">int<\/span><br \/>\n    prev <span class=\"token operator\">*<\/span>Node<br \/>\n    next <span class=\"token operator\">*<\/span>Node<br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p><span class=\"token keyword\">type<\/span> LRUCache <span class=\"token keyword\">struct<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    capacity <span class=\"token builtin\">int<\/span><br \/>\n    size     <span class=\"token builtin\">int<\/span><br \/>\n    cache    <span class=\"token keyword\">map<\/span><span class=\"token punctuation\">[<\/span><span class=\"token builtin\">int<\/span><span class=\"token punctuation\">]<\/span><span class=\"token operator\">*<\/span>Node<br \/>\n    head     <span class=\"token operator\">*<\/span>Node<br \/>\n    tail     <span class=\"token operator\">*<\/span>Node<br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p><span class=\"token keyword\">func<\/span> <span class=\"token function\">Constructor<\/span><span class=\"token punctuation\">(<\/span>capacity <span class=\"token builtin\">int<\/span><span class=\"token punctuation\">)<\/span> LRUCache <span class=\"token punctuation\">{<\/span><br \/>\n    head <span class=\"token operator\">:&#061;<\/span> <span class=\"token operator\">&amp;<\/span>Node<span class=\"token punctuation\">{<\/span><span class=\"token punctuation\">}<\/span><br \/>\n    tail <span class=\"token operator\">:&#061;<\/span> <span class=\"token operator\">&amp;<\/span>Node<span class=\"token punctuation\">{<\/span><span class=\"token punctuation\">}<\/span><br \/>\n    head<span class=\"token punctuation\">.<\/span>next <span class=\"token operator\">&#061;<\/span> tail<br \/>\n    tail<span class=\"token punctuation\">.<\/span>prev <span class=\"token operator\">&#061;<\/span> head<br \/>\n    <span class=\"token keyword\">return<\/span> LRUCache<span class=\"token punctuation\">{<\/span><br \/>\n        capacity<span class=\"token punctuation\">:<\/span> capacity<span class=\"token punctuation\">,<\/span><br \/>\n        cache<span class=\"token punctuation\">:<\/span>    <span class=\"token function\">make<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">map<\/span><span class=\"token punctuation\">[<\/span><span class=\"token builtin\">int<\/span><span class=\"token punctuation\">]<\/span><span class=\"token operator\">*<\/span>Node<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span><br \/>\n        head<span class=\"token punctuation\">:<\/span>     head<span class=\"token punctuation\">,<\/span><br \/>\n        tail<span class=\"token punctuation\">:<\/span>     tail<span class=\"token punctuation\">,<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p><span class=\"token keyword\">func<\/span> <span class=\"token punctuation\">(<\/span>this <span class=\"token operator\">*<\/span>LRUCache<span class=\"token punctuation\">)<\/span> <span class=\"token function\">moveToTail<\/span><span class=\"token punctuation\">(<\/span>node <span class=\"token operator\">*<\/span>Node<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    node<span class=\"token punctuation\">.<\/span>prev<span class=\"token punctuation\">.<\/span>next <span class=\"token operator\">&#061;<\/span> node<span class=\"token punctuation\">.<\/span>next<br \/>\n    node<span class=\"token punctuation\">.<\/span>next<span class=\"token punctuation\">.<\/span>prev <span class=\"token operator\">&#061;<\/span> node<span class=\"token punctuation\">.<\/span>prev<\/p>\n<p>    node<span class=\"token punctuation\">.<\/span>prev <span class=\"token operator\">&#061;<\/span> this<span class=\"token punctuation\">.<\/span>tail<span class=\"token punctuation\">.<\/span>prev<br \/>\n    node<span class=\"token punctuation\">.<\/span>next <span class=\"token operator\">&#061;<\/span> this<span class=\"token punctuation\">.<\/span>tail<br \/>\n    this<span class=\"token punctuation\">.<\/span>tail<span class=\"token punctuation\">.<\/span>prev<span class=\"token punctuation\">.<\/span>next <span class=\"token operator\">&#061;<\/span> node<br \/>\n    this<span class=\"token punctuation\">.<\/span>tail<span class=\"token punctuation\">.<\/span>prev <span class=\"token operator\">&#061;<\/span> node<br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p><span class=\"token keyword\">func<\/span> <span class=\"token punctuation\">(<\/span>this <span class=\"token operator\">*<\/span>LRUCache<span class=\"token punctuation\">)<\/span> <span class=\"token function\">addToTail<\/span><span class=\"token punctuation\">(<\/span>node <span class=\"token operator\">*<\/span>Node<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    node<span class=\"token punctuation\">.<\/span>prev <span class=\"token operator\">&#061;<\/span> this<span class=\"token punctuation\">.<\/span>tail<span class=\"token punctuation\">.<\/span>prev<br \/>\n    node<span class=\"token punctuation\">.<\/span>next <span class=\"token operator\">&#061;<\/span> this<span class=\"token punctuation\">.<\/span>tail<br \/>\n    this<span class=\"token punctuation\">.<\/span>tail<span class=\"token punctuation\">.<\/span>prev<span class=\"token punctuation\">.<\/span>next <span class=\"token operator\">&#061;<\/span> node<br \/>\n    this<span class=\"token punctuation\">.<\/span>tail<span class=\"token punctuation\">.<\/span>prev <span class=\"token operator\">&#061;<\/span> node<br \/>\n    this<span class=\"token punctuation\">.<\/span>cache<span class=\"token punctuation\">[<\/span>node<span class=\"token punctuation\">.<\/span>key<span class=\"token punctuation\">]<\/span> <span class=\"token operator\">&#061;<\/span> node<br \/>\n    this<span class=\"token punctuation\">.<\/span>size<span class=\"token operator\">&#043;&#043;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p><span class=\"token keyword\">func<\/span> <span class=\"token punctuation\">(<\/span>this <span class=\"token operator\">*<\/span>LRUCache<span class=\"token punctuation\">)<\/span> <span class=\"token function\">removeHead<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token keyword\">if<\/span> this<span class=\"token punctuation\">.<\/span>head<span class=\"token punctuation\">.<\/span>next <span class=\"token operator\">&#061;&#061;<\/span> this<span class=\"token punctuation\">.<\/span>tail <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token keyword\">return<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n    node <span class=\"token operator\">:&#061;<\/span> this<span class=\"token punctuation\">.<\/span>head<span class=\"token punctuation\">.<\/span>next<br \/>\n    this<span class=\"token punctuation\">.<\/span>head<span class=\"token punctuation\">.<\/span>next <span class=\"token operator\">&#061;<\/span> node<span class=\"token punctuation\">.<\/span>next<br \/>\n    node<span class=\"token punctuation\">.<\/span>next<span class=\"token punctuation\">.<\/span>prev <span class=\"token operator\">&#061;<\/span> this<span class=\"token punctuation\">.<\/span>head<br \/>\n    <span class=\"token function\">delete<\/span><span class=\"token punctuation\">(<\/span>this<span class=\"token punctuation\">.<\/span>cache<span class=\"token punctuation\">,<\/span> node<span class=\"token punctuation\">.<\/span>key<span class=\"token punctuation\">)<\/span><br \/>\n    this<span class=\"token punctuation\">.<\/span>size<span class=\"token operator\">&#8212;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p><span class=\"token keyword\">func<\/span> <span class=\"token punctuation\">(<\/span>this <span class=\"token operator\">*<\/span>LRUCache<span class=\"token punctuation\">)<\/span> <span class=\"token function\">Get<\/span><span class=\"token punctuation\">(<\/span>key <span class=\"token builtin\">int<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token builtin\">int<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    node<span class=\"token punctuation\">,<\/span> ok <span class=\"token operator\">:&#061;<\/span> this<span class=\"token punctuation\">.<\/span>cache<span class=\"token punctuation\">[<\/span>key<span class=\"token punctuation\">]<\/span><br \/>\n    <span class=\"token keyword\">if<\/span> <span class=\"token operator\">!<\/span>ok <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token keyword\">return<\/span> <span class=\"token operator\">&#8211;<\/span><span class=\"token number\">1<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n    this<span class=\"token punctuation\">.<\/span><span class=\"token function\">moveToTail<\/span><span class=\"token punctuation\">(<\/span>node<span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token keyword\">return<\/span> node<span class=\"token punctuation\">.<\/span>val<br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p><span class=\"token keyword\">func<\/span> <span class=\"token punctuation\">(<\/span>this <span class=\"token operator\">*<\/span>LRUCache<span class=\"token punctuation\">)<\/span> <span class=\"token function\">Put<\/span><span class=\"token punctuation\">(<\/span>key <span class=\"token builtin\">int<\/span><span class=\"token punctuation\">,<\/span> value <span class=\"token builtin\">int<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token keyword\">if<\/span> node<span class=\"token punctuation\">,<\/span> ok <span class=\"token operator\">:&#061;<\/span> this<span class=\"token punctuation\">.<\/span>cache<span class=\"token punctuation\">[<\/span>key<span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">;<\/span> ok <span class=\"token punctuation\">{<\/span><br \/>\n        node<span class=\"token punctuation\">.<\/span>val <span class=\"token operator\">&#061;<\/span> value<br \/>\n        this<span class=\"token punctuation\">.<\/span><span class=\"token function\">moveToTail<\/span><span class=\"token punctuation\">(<\/span>node<span class=\"token punctuation\">)<\/span><br \/>\n        <span class=\"token keyword\">return<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n    <span class=\"token keyword\">if<\/span> this<span class=\"token punctuation\">.<\/span>size <span class=\"token operator\">&#061;&#061;<\/span> this<span class=\"token punctuation\">.<\/span>capacity <span class=\"token punctuation\">{<\/span><br \/>\n        this<span class=\"token punctuation\">.<\/span><span class=\"token function\">removeHead<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n    newNode <span class=\"token operator\">:&#061;<\/span> <span class=\"token operator\">&amp;<\/span>Node<span class=\"token punctuation\">{<\/span>key<span class=\"token punctuation\">:<\/span> key<span class=\"token punctuation\">,<\/span> val<span class=\"token punctuation\">:<\/span> value<span class=\"token punctuation\">}<\/span><br \/>\n    this<span class=\"token punctuation\">.<\/span><span class=\"token function\">addToTail<\/span><span class=\"token punctuation\">(<\/span>newNode<span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token punctuation\">}<\/span>\n <\/li>\n<\/ul>\n<h4>\u6ce8\u610f<\/h4>\n<ul>\n<li>\u8fd9\u91cc\u6211\u5fc5\u987b\u63d0\u9192\u4e00\u53e5&#xff0c;\u5728 Golang \u4e2d&#xff0c;GC&#xff08;\u5783\u573e\u56de\u6536&#xff09;\u673a\u5236\u7684\u5224\u65ad\u6807\u51c6\u662f&#xff1a;\u5982\u679c\u4e00\u5757\u5185\u5b58\u5730\u5740\u4e0d\u518d\u88ab\u4efb\u4f55\u53d8\u91cf\u6216\u6570\u636e\u7ed3\u6784\u5f15\u7528&#xff0c;\u5b83\u624d\u4f1a\u88ab\u56de\u6536\u3002\u5728\u6211\u4eec\u7684 LRU \u5b9e\u73b0\u4e2d&#xff0c;\u5f53\u7f13\u5b58\u6ee1\u4e86\u9700\u8981\u5220\u9664\u5c3e\u8282\u70b9\u65f6&#xff0c;\u6211\u4eec\u786e\u5b9e\u628a\u8282\u70b9\u4ece\u53cc\u5411\u94fe\u8868\u4e2d\u79fb\u9664\u4e86&#xff0c;\u4f46\u8fd9\u4e2a\u8282\u70b9\u7684\u5730\u5740\u8fd8\u5b58\u50a8\u5728 map \u91cc\u9762\u2014\u2014map \u4ecd\u7136\u6301\u6709\u8fd9\u4e2a key \u548c\u5bf9\u5e94\u7684\u6307\u9488\u3002\u8fd9\u65f6\u5019 GC \u770b\u5230 map \u8fd8\u5728\u5f15\u7528\u8fd9\u5757\u5185\u5b58&#xff0c;\u5c31\u8ba4\u4e3a\u5b83\u8fd8\u5728\u4f7f\u7528\u4e2d&#xff0c;\u4e0d\u4f1a\u56de\u6536\u5b83\u3002\u4e5f\u5c31\u662f\u8bf4&#xff0c;\u867d\u7136\u6211\u4eec\u4ece\u94fe\u8868\u4e2d\u5220\u9664\u4e86\u5c3e\u8282\u70b9&#xff0c;\u4f46\u5982\u679c\u4e0d\u505a\u989d\u5916\u5904\u7406&#xff0c;\u8fd9\u5757\u5185\u5b58\u5c31\u6c38\u8fdc\u65e0\u6cd5\u88ab\u91ca\u653e&#xff0c;\u8fd9\u5c31\u53d1\u751f\u4e86\u5185\u5b58\u6cc4\u6f0f\u3002\u968f\u7740\u4e0d\u65ad\u7684\u5220\u9664\u548c\u63d2\u5165&#xff0c;map \u4e2d\u4f1a\u79ef\u7d2f\u5927\u91cf\u5df2\u7ecf\u4e0d\u5728\u94fe\u8868\u4e2d\u7684&#034;\u50f5\u5c38\u8282\u70b9&#034;&#xff0c;\u5185\u5b58\u5360\u7528\u53ea\u589e\u4e0d\u51cf&#xff0c;\u6700\u7ec8\u53ef\u80fd\u5bfc\u81f4 OOM&#xff08;Out Of Memory&#xff09;\u3002\u6240\u4ee5&#xff0c;\u5728\u5220\u9664\u5c3e\u8282\u70b9\u7684\u65f6\u5019&#xff0c;\u6211\u4eec\u4e00\u5b9a\u8981\u4f7f\u7528 Golang \u7684 delete \u5185\u7f6e\u51fd\u6570&#xff0c;\u4ece map \u4e2d\u5220\u9664\u5bf9\u5e94\u7684 key&#xff0c;\u8fd9\u6837 map \u4e0d\u518d\u5f15\u7528\u8be5\u8282\u70b9&#xff0c;\u94fe\u8868\u4e2d\u4e5f\u6ca1\u6709\u5f15\u7528&#xff0c;GC \u5c31\u80fd\u6b63\u5e38\u56de\u6536\u8fd9\u5757\u5185\u5b58&#xff0c;\u4fdd\u8bc1\u7a0b\u5e8f\u7684\u5065\u58ee\u6027\u3002<\/li>\n<\/ul>\n<h3>\u7ed3\u8bed<\/h3>\n<p>\u672c\u6587\u662f \u300a\u7b97\u6cd5\u9898\u76ee\u89e3\u6790\u7cfb\u5217\u300b \u7684\u7b2c [24] \u7bc7&#xff0c;\u672c\u7cfb\u5217\u5c06\u6301\u7eed\u66f4\u65b0&#xff0c;\u6bcf\u7bc7\u90fd\u63d0\u4f9b\u6e05\u6670\u7684\u601d\u8def\u4e0e\u7f16\u7a0b\u8bed\u8a00\u5b9e\u73b0\u3002\u6b22\u8fce\u5173\u6ce8&#xff0c;\u7b2c\u4e00\u65f6\u95f4\u83b7\u53d6\u66f4\u65b0\u3002\u5982\u679c\u4f60\u6709\u60f3\u770b\u7684\u9898\u76ee&#xff0c;\u4e5f\u53ef\u4ee5\u5728\u8bc4\u8bba\u533a\u7559\u8a00\u544a\u8bc9\u6211\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>146. LRU \u7f13\u5b58 \u6587\u7ae0\u76ee\u5f55[146. LRU \u7f13\u5b58](https:\/\/leetcode.cn\/problems\/lru-cache\/)\u601d\u8defJava\u4ee3\u7801Python\u4ee3\u7801Golang\u4ee3\u7801\u6ce8\u610f\u7ed3\u8bed\u8bf7\u4f60\u8bbe\u8ba1\u5e76\u5b9e\u73b0\u4e00\u4e2a\u6ee1\u8db3 LRU (\u6700\u8fd1\u6700\u5c11\u4f7f\u7528) \u7f13\u5b58 \u7ea6\u675f\u7684\u6570\u636e\u7ed3\u6784\u3002<br \/>\n\u5b9e\u73b0 LRUCache \u7c7b&#xff1a;<br \/>\nLRUCache(int capacity) \u4ee5 \u6b63\u6574\u6570 \u4f5c\u4e3a\u5bb9\u91cf capacity \u521d\u59cb\u5316 LRU \u7f13\u5b58int get(int key) \u5982\u679c\u5173\u952e\u5b57 key \u5b58<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[746,99,427,398,2640],"topic":[],"class_list":["post-99629","post","type-post","status-publish","format-standard","hentry","category-server","tag-golang","tag-java","tag-427","tag-398","tag-2640"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u4e00\u5929\u4e00\u9053\u7b97\u6cd5\u9898\uff0824\uff09\uff1aLRU\u7f13\u5b58\u673a\u5236 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/99629.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u4e00\u5929\u4e00\u9053\u7b97\u6cd5\u9898\uff0824\uff09\uff1aLRU\u7f13\u5b58\u673a\u5236 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"146. LRU \u7f13\u5b58 \u6587\u7ae0\u76ee\u5f55[146. LRU \u7f13\u5b58](https:\/\/leetcode.cn\/problems\/lru-cache\/)\u601d\u8defJava\u4ee3\u7801Python\u4ee3\u7801Golang\u4ee3\u7801\u6ce8\u610f\u7ed3\u8bed\u8bf7\u4f60\u8bbe\u8ba1\u5e76\u5b9e\u73b0\u4e00\u4e2a\u6ee1\u8db3 LRU (\u6700\u8fd1\u6700\u5c11\u4f7f\u7528) \u7f13\u5b58 \u7ea6\u675f\u7684\u6570\u636e\u7ed3\u6784\u3002 \u5b9e\u73b0 LRUCache \u7c7b&#xff1a; LRUCache(int capacity) \u4ee5 \u6b63\u6574\u6570 \u4f5c\u4e3a\u5bb9\u91cf capacity \u521d\u59cb\u5316 LRU \u7f13\u5b58int get(int key) \u5982\u679c\u5173\u952e\u5b57 key \u5b58\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/99629.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-09-02T15:56:46+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/99629.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/99629.html\",\"name\":\"\u4e00\u5929\u4e00\u9053\u7b97\u6cd5\u9898\uff0824\uff09\uff1aLRU\u7f13\u5b58\u673a\u5236 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-09-02T15:56:46+00:00\",\"dateModified\":\"2026-09-02T15:56:46+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/99629.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/99629.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/99629.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u4e00\u5929\u4e00\u9053\u7b97\u6cd5\u9898\uff0824\uff09\uff1aLRU\u7f13\u5b58\u673a\u5236\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u4e00\u5929\u4e00\u9053\u7b97\u6cd5\u9898\uff0824\uff09\uff1aLRU\u7f13\u5b58\u673a\u5236 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/99629.html","og_locale":"zh_CN","og_type":"article","og_title":"\u4e00\u5929\u4e00\u9053\u7b97\u6cd5\u9898\uff0824\uff09\uff1aLRU\u7f13\u5b58\u673a\u5236 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"146. LRU \u7f13\u5b58 \u6587\u7ae0\u76ee\u5f55[146. LRU \u7f13\u5b58](https:\/\/leetcode.cn\/problems\/lru-cache\/)\u601d\u8defJava\u4ee3\u7801Python\u4ee3\u7801Golang\u4ee3\u7801\u6ce8\u610f\u7ed3\u8bed\u8bf7\u4f60\u8bbe\u8ba1\u5e76\u5b9e\u73b0\u4e00\u4e2a\u6ee1\u8db3 LRU (\u6700\u8fd1\u6700\u5c11\u4f7f\u7528) \u7f13\u5b58 \u7ea6\u675f\u7684\u6570\u636e\u7ed3\u6784\u3002 \u5b9e\u73b0 LRUCache \u7c7b&#xff1a; LRUCache(int capacity) \u4ee5 \u6b63\u6574\u6570 \u4f5c\u4e3a\u5bb9\u91cf capacity \u521d\u59cb\u5316 LRU \u7f13\u5b58int get(int key) \u5982\u679c\u5173\u952e\u5b57 key \u5b58","og_url":"https:\/\/www.wsisp.com\/helps\/99629.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-09-02T15:56:46+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"4 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/99629.html","url":"https:\/\/www.wsisp.com\/helps\/99629.html","name":"\u4e00\u5929\u4e00\u9053\u7b97\u6cd5\u9898\uff0824\uff09\uff1aLRU\u7f13\u5b58\u673a\u5236 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-09-02T15:56:46+00:00","dateModified":"2026-09-02T15:56:46+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/99629.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/99629.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/99629.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u4e00\u5929\u4e00\u9053\u7b97\u6cd5\u9898\uff0824\uff09\uff1aLRU\u7f13\u5b58\u673a\u5236"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/99629","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=99629"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/99629\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=99629"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=99629"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=99629"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=99629"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}