146 LRU Cache

Posted mengchunchen

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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

实现LRU
用map+双向链表


C++:
 1 class LRUCache {
 2 private:
 3     struct Node{
 4         int key ;
 5         int value ;
 6         Node* left ;
 7         Node* right ;
 8         Node(int k , int v): key(k) , value(v) , left(NULL) , right(NULL) {}
 9     };
10     
11     int capacity ;
12     unordered_map<int,Node*> hash ;
13     Node* head ;
14     Node* tail ;
15     
16     void deleteNode(Node * node){
17         node->left->right = node->right ;
18         node->right->left = node->left ;
19     }
20     
21     void insertToTail(Node * node){
22         Node* last = tail->left ;
23         node->right = tail ;
24         node->left = last ;
25         tail->left = node ;
26         last->right = node ;
27     }
28     
29 public:
30     LRUCache(int capacity) {
31         this->capacity = capacity ;
32         this->head = new Node(0,0) ;
33         this->tail = new Node(0,0) ;
34         head->right = this->tail ;
35         tail->left = this->head ;
36     }
37     
38     int get(int key) {
39         if (hash.count(key) == 0){
40             return -1 ;
41         }else{
42             deleteNode(hash[key]) ;
43             insertToTail(hash[key]) ;
44             return hash[key]->value ;
45         }
46     }
47     
48     void put(int key, int value) {
49         if (hash.count(key) != 0){
50             deleteNode(hash[key]) ;
51             insertToTail(hash[key]) ;
52             hash[key]->value = value ;
53         }else{
54             if (hash.size() < this->capacity){
55                 Node* node = new Node(key,value) ;
56                 insertToTail(node) ;
57                 hash[key] = node ;
58             }else{
59                 Node* node = head->right ;
60                 deleteNode(node) ;
61                 insertToTail(node) ;
62                 hash.erase(node->key) ;
63                 hash[key] = node ;
64                 node->key = key ;
65                 node->value = value ;
66             }
67         }
68     }
69 };
70 
71 /**
72  * Your LRUCache object will be instantiated and called as such:
73  * LRUCache obj = new LRUCache(capacity);
74  * int param_1 = obj.get(key);
75  * obj.put(key,value);
76  */

 

 









以上是关于146 LRU Cache的主要内容,如果未能解决你的问题,请参考以下文章

leetcode@ [146] LRU Cache (TreeMap)

146. LRU Cache

146. LRU Cache

leetcode146 LRU Cache

146. LRU Cache

146 LRU Cache