{"id":105586,"date":"2020-05-23T00:56:43","date_gmt":"2020-05-22T16:56:43","guid":{"rendered":"http:\/\/4563.org\/?p=105586"},"modified":"2020-05-23T00:56:43","modified_gmt":"2020-05-22T16:56:43","slug":"leetcode-lintcode-%e9%a2%98%e8%a7%a3-lru-%e7%bc%93%e5%ad%98%e7%ad%96%e7%95%a5-%c2%b7-lru-cache","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=105586","title":{"rendered":"[leetcode\/lintcode \u9898\u89e3] LRU \u7f13\u5b58\u7b56\u7565 \u00b7 LRU Cache"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  [leetcode\/lintcode \u9898\u89e3] LRU \u7f13\u5b58\u7b56\u7565 \u00b7 LRU Cache               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : hakunamatata11 <\/span>  <span><i><\/i> 2<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>[\u9898\u76ee\u63cf\u8ff0]<\/p>\n<p>\u4e3a\u6700\u8fd1\u6700\u5c11\u4f7f\u7528\uff08 LRU \uff09\u7f13\u5b58\u7b56\u7565\u8bbe\u8ba1\u4e00\u4e2a\u6570\u636e\u7ed3\u6784\uff0c\u5b83\u5e94\u8be5\u652f\u6301\u4ee5\u4e0b\u64cd\u4f5c\uff1a\u83b7\u53d6\u6570\u636e\u548c\u5199\u5165\u6570\u636e\u3002<\/p>\n<ul>\n<li>\n<p>get(key) \u83b7\u53d6\u6570\u636e\uff1a\u5982\u679c\u7f13\u5b58\u4e2d\u5b58\u5728 key\uff0c\u5219\u83b7\u53d6\u5176\u6570\u636e\u503c\uff08\u901a\u5e38\u662f\u6b63\u6570\uff09\uff0c\u5426\u5219\u8fd4\u56de-1 \u3002<\/p>\n<\/li>\n<li>\n<p>set(key, value) \u5199\u5165\u6570\u636e\uff1a\u5982\u679c key \u8fd8\u6ca1\u6709\u5728\u7f13\u5b58\u4e2d\uff0c\u5219\u5199\u5165\u5176\u6570\u636e\u503c\u3002\u5f53\u7f13\u5b58\u8fbe\u5230\u4e0a\u9650\uff0c\u5b83\u5e94\u8be5\u5728\u5199\u5165\u65b0\u6570\u636e\u4e4b\u524d\u5220\u9664\u6700\u8fd1\u6700\u5c11\u4f7f\u7528\u7684\u6570\u636e\u7528\u6765\u817e\u51fa\u7a7a\u95f2\u4f4d\u7f6e\u3002<\/p>\n<\/li>\n<\/ul>\n<p>\u6700\u7ec8, \u4f60\u9700\u8981\u8fd4\u56de\u6bcf\u6b21 get \u7684\u6570\u636e.<\/p>\n<p>\u5728\u7ebf\u8bc4\u6d4b\u5730\u5740: https:\/\/www.lintcode.com\/problem\/lru-cache\/?utm_source=sc-v2ex-fks0525<\/p>\n<p>[\u6837\u4f8b] \u6837\u4f8b 1:<\/p>\n<pre><code>\u8f93\u5165\uff1a LRUCache(2) set(2, 1) set(1, 1) get(2) set(4, 1) get(1) get(2) \u8f93\u51fa\uff1a[1,-1,1] \u89e3\u91ca\uff1a cache \u4e0a\u9650\u4e3a 2\uff0cset(2,1)\uff0cset(1, 1)\uff0cget(2) \u7136\u540e\u8fd4\u56de 1\uff0cset(4,1) \u7136\u540e delete (1,1)\uff0c\u56e0\u4e3a \uff08 1,1 \uff09\u6700\u5c11\u4f7f\u7528\uff0cget(1) \u7136\u540e\u8fd4\u56de -1\uff0cget(2) \u7136\u540e\u8fd4\u56de 1 \u3002 <\/code><\/pre>\n<p>\u6837\u4f8b 2:<\/p>\n<pre><code>\u8f93\u5165\uff1a LRUCache(1) set(2, 1) get(2) set(3, 2) get(2) get(3) \u8f93\u51fa\uff1a[1,-1,2] \u89e3\u91ca\uff1a cache \u4e0a\u9650\u4e3a 1\uff0cset(2,1)\uff0cget(2) \u7136\u540e\u8fd4\u56de 1\uff0cset(3,2) \u7136\u540e delete (2,1)\uff0cget(2) \u7136\u540e\u8fd4\u56de -1\uff0cget(3) \u7136\u540e\u8fd4\u56de 2 \u3002 <\/code><\/pre>\n<p>[\u9898\u89e3] Singly Linked List \u7684\u7248\u672c<\/p>\n<pre><code>public class LRUCache {     class ListNode {         public int key, val;         public ListNode next;                  public ListNode(int key, int val) {             this.key = key;             this.val = val;             this.next = null;         }     }          private int capacity, size;     private ListNode dummy, tail;     private Map&lt;Integer, ListNode&gt; keyToPrev;      \/*     * @param capacity: An integer     *\/     public LRUCache(int capacity) {         this.capacity = capacity;         this.keyToPrev = new HashMap&lt;Integer, ListNode&gt;();         this.dummy = new ListNode(0, 0);         this.tail = this.dummy;     }      private void moveToTail(int key) {         ListNode prev = keyToPrev.get(key);         ListNode curt = prev.next;                  if (tail == curt) {             return;         }                  prev.next = prev.next.next;         tail.next = curt;                  if (prev.next != null) {             keyToPrev.put(prev.next.key, prev);         }         keyToPrev.put(curt.key, tail);                  tail = curt;     }          \/*      * @param key: An integer      * @return: An integer      *\/     public int get(int key) {         if (!keyToPrev.containsKey(key)) {             return -1;         }                  moveToTail(key);                  \/\/ the key has been moved to the end         return tail.val;     }          \/*      * @param key: An integer      * @param value: An integer      * @return: nothing      *\/     public void set(int key, int value) {         \/\/ get method will move the key to the end of the linked list         if (get(key) != -1) {             ListNode prev = keyToPrev.get(key);             prev.next.val = value;             return;         }                  if (size &lt; capacity) {             size++;             ListNode curt = new ListNode(key, value);             tail.next = curt;             keyToPrev.put(key, tail);                          tail = curt;             return;         }                  \/\/ replace the first node with new key, value         ListNode first = dummy.next;         keyToPrev.remove(first.key);                  first.key = key;         first.val = value;         keyToPrev.put(key, dummy);                  moveToTail(key);     } } <\/code><\/pre>\n<blockquote>\n<h2>\u300a\u4e5d\u7ae0\u7b97\u6cd5\u73ed 2020 \u7248\u300b<\/h2>\n<\/blockquote>\n<p>\u8bfe\u7a0b\u9002\u914d C++\/Java\/Python \u7b49\u4e3b\u6d41\u7f16\u7a0b\u8bed\u8a00\uff0c30 \u5929\u7cbe\u901a 57 \u4e2a\u6838\u5fc3\u9ad8\u9891\u8003\u70b9\uff0c9 \u62db\u51fb\u7834 FLAG \u3001BATJ \u7b97\u6cd5\u9762\u8bd5\u3002<\/p>\n<p><strong>\u5185\u5bb9\u4eae\u70b9\uff1a<\/strong><\/p>\n<ul>\n<li>\n<p>\u8bfe\u7a0b\u8986\u76d6 90%\u56fd\u5185\u5916\u4e00\u7ebf\u5927\u5382\u7b97\u6cd5\u9762\u8bd5\u9ad8\u9891\u8003\u70b9<\/p>\n<\/li>\n<li>\n<p>2020 \u590f\u79cb\u62db\u6700\u65b0\u5927\u5382\u9762\u8bd5\u7b97\u6cd5\u9898 80+\u5b9e\u6218\u8bb2\u89e3<\/p>\n<\/li>\n<li>\n<p>\u89c4\u8303\u7f16\u7a0b\u7ec6\u8282\uff0c\u5b9e\u8d28\u6027\u63d0\u5347 Coding \u80fd\u529b\u907f\u514d\u9762\u8bd5\u201c\u9690\u5f62\u5751\u201d<\/p>\n<\/li>\n<li>\n<p>\u8bfe\u7a0b\u4f53\u7cfb\u5b8c\u7f8e\u5339\u914d\u5927\u5382\u9762\u8bd5\u8003\u70b9\uff0c\u7b97\u6cd5\u9762\u8bd5\u4ece\u5bb9\u5e94\u5bf9<\/p>\n<\/li>\n<\/ul>\n<p>\u6233\u6211\u7acb\u5373\u514d\u8d39\u62a5\u540d<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>0<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>[leetcode\/lintcod&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\/105586"}],"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=105586"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/105586\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=105586"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=105586"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=105586"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}