LeetCode-146.LRU Cache

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

get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.
put(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.

Follow up:
Could you do both operations in O(1) time complexity?

Example:

LRUCache cache = new LRUCache( 2 /* capacity */ );
cache.put(1, 1);
cache.put(2, 2);
cache.get(1);       // returns 1
cache.put(3, 3);    // evicts key 2
cache.get(2);       // returns -1 (not found)
cache.put(4, 4);    // evicts key 1
cache.get(1);       // returns -1 (not found)
cache.get(3);       // returns 3
cache.get(4);       // returns 4

使用Java的LinkedHashMap实现
class LRUCache {//mytip
    LinkedHashMap<Integer,Integer> lru;
    int cap = 0;
    
    public LRUCache(int capacity) {
        //构造函数 accessOrder = true表示按照访问次序,为false表示按照插入次序
        lru=new LinkedHashMap<Integer,Integer>(0,0.75f,true){

            @Override
            protected boolean removeEldestEntry(Map.Entry<Integer, Integer> eldest) {
                return size()>cap;
            }

        };
        cap = capacity;
    }
    
    public int get(int key) {
        Integer re = lru.get(key);
        return null==re?-1:re;
    }
    
    public void put(int key, int value) {
        lru.put(key,value);
    }
}

 

自己实现Map+双向链表

 1 class LRUCache {
 2     Map<Integer,Node> map;
 3     int cap=0;
 4     Node head =null;
 5     Node tail = null;
 6     
 7     public LRUCache(int capacity) {
 8         map = new HashMap<>();
 9         cap = capacity;
10         head = new Node(0,0);
11         tail = new Node(0,0);
12         head.next =tail;
13         tail.pre =head;
14     }
15     
16     public int get(int key) {
17         Node node = map.get(key);
18         if(null!=node){
19             nodeToHead(node);
20             return node.value;
21         }
22         return -1;
23     }
24     
25     public void put(int key, int value) {
26         Node node = map.get(key);
27         if(null!=node){
28             node.value = value;
29             nodeToHead(node); 
30             return ;
31         }
32         if(map.size()==cap){
33             node  = tail.pre;
34             node.pre.next = tail;
35             tail.pre = node.pre;
36             map.remove(node.key);
37         }
38         node = new Node(key,value);
39         node.next = head.next;
40         node.pre = head;
41         head.next = node;
42         node.next.pre = node;
43         map.put(key,node);
44         
45     }
46     
47     private void nodeToHead(Node node){
48         node.pre.next = node.next;
49         node.next.pre = node.pre;
50         node.next = head.next;
51         node.pre = head;
52         node.next.pre =node;
53         head.next = node;
54     }
55 }
56 
57 class Node{
58     int key;
59     int value;
60     Node pre;
61     Node next;
62     public Node(int key,int value){
63         this.key= key;
64         this.value  =value;
65         pre = null;
66         next = null;
67     }
68 }

 

posted @ 2019-04-07 13:46  月半榨菜  阅读(130)  评论(0编辑  收藏  举报