Java for LeetCode 146 LRU Cache 【HARD】

來源:互聯網
上載者:User

標籤:

Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set.

get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.
set(key, value) - Set or insert the value if the key is not already present. When the cache reached its capacity, it should invalidate the least recently used item before inserting a new item.

解題思路:

實現LRU演算法的緩衝類,由於cache會有頻繁的讀改操作,所以要有合適的資料結構來讓 set 和 get 的複雜度很小,最好近 O(1)。
主要有兩種思路:

一、用 Splay 實現,Splay 是棵 BST,同時在尋找和修改的時候會讓那個節點上浮到根節點,不過操作都是 O(log(n)) 層級的,而且有個問題,就是這棵樹可能會變成一條鏈(正常節點都是按查詢頻率從上到下,所以很快,均攤小於 O(log(n)))。Splay 實現過於複雜,這裡就不給出。
二、用雙鏈表和 HashMap 實現。

鏈表的作用是記錄節點的使用順序。正常情況下 LRU 都是用這種做法的。
HashMap 實現用 key 找到 List 中的節點對象,找不到就在 List 中增加節點,並插入 HashMap。
按照要求得到或修改節點的 value。
修改節點的使用時間,也就是把 List 中的節點拉到 List 頭部。
在第一步時如果節點個數大於可用容量,就將 List 的最後一個節點刪去。
JAVA實現如下:

package oj.leetcode;import java.util.*;public class LRUCache {    private int capacity;    private Node head, tail;    private HashMap<Integer, Node> keyNodeMap;    public LRUCache(int capacity) {        this.capacity = capacity;        head = new Node(-1, -1);        tail = new Node(0, 0);        head.next = tail;        tail.pre = head;        this.keyNodeMap = new HashMap<Integer, Node>();    }    public int get(int key) {        Node node = keyNodeMap.get(key);        if (node != null) {            moveToHead(node);            return node.value;        }        return -1;    }    public void set(int key, int value) {        Node node = null;        if (keyNodeMap.containsKey(key)) {            node = keyNodeMap.get(key);            node.value = value;        } else {            node = new Node(key, value);            if (keyNodeMap.size() == capacity) {                keyNodeMap.remove(removeTail());            }            keyNodeMap.put(key, node);        }        moveToHead(node);    }    private void moveToHead(Node node) {        if (node.pre != null || node.next != null) {            node.next.pre = node.pre;            node.pre.next = node.next;        }        node.next = head.next;        head.next.pre = node;        node.pre = head;        head.next = node;    }    private int removeTail() {        int lastKey = -1;        if (tail.pre != head) {            Node lastNode = tail.pre;            lastKey = lastNode.key;            lastNode.pre.next = tail;            tail.pre = lastNode.pre;            lastNode = null;        }        return lastKey;    }    class Node{        int key;        int value;        Node pre;        Node next;        public Node(int k, int v) {            key = k;            value = v;        }    }}

 

Java for LeetCode 146 LRU Cache 【HARD】

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.