码迷,mamicode.com
首页 > 系统相关 > 详细

146 LRU Cache

时间:2018-10-25 17:08:06      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:tail   otherwise   obj   链表   style   for   ota   ==   other   

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

标签:tail   otherwise   obj   链表   style   for   ota   ==   other   

原文地址:https://www.cnblogs.com/mengchunchen/p/9850908.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!