leetcode:. 146 LRU cache mechanism

Subject description:

Use your disposal data structures, design and implement an LRU (least recently used) cache mechanism. It should support the following: Obtain the data and write data to get put.

Get data get (key) - If the key (key) is present in the cache, the acquisition value of the key (always positive), otherwise -1.
Write data put (key, value) - If the key is not present, the value of the data which is written. When the cache capacity limit is reached, it should delete the least recently used data values before writing new data to make room for new data value.

 

If you can do both operations in O (1) time complexity?

 

Example:

LRUCache cache = new LRUCache (2 / * buffer size * /);

cache.put (. 1,. 1);
cache.put (2, 2);
cache.get (. 1); // returns. 1
cache.put (. 3,. 3); // This operation will make the key 2 invalid
cache. get (2); // returns -1 (not found)
cache.put (. 4,. 4); // this operation will cause an invalid key
cache.get (1); // returns -1 (not found)
Cache .get (3); // returns. 3
cache.get (4); // returns 4

 

Analysis of ideas:

The least recently used caching mechanism, before learning to find out about the operating system. Need to be able to complete in (n) time complexity O write data and retrieve data. Consider the use of hash, due to the need to maintain the cache is the latest input node will move it to the beginning of the array do move operations require O (n), so use this list to store the cache, you can ensure that O insert and delete operations ( 1) time. Re-use a map storing appeared node can find the node appeared in O (1) time and date. It should however be found in this node simultaneously find their place in the cache, so convenient insertion deleted, so this map <int, list>, the node soon as the value corresponding to the pointer stored. Note here that the map used in the list is an iterator, convenient and direct calls begin and end functions.

 

Code:

 1 class LRUCache {
 2 public:
 3     LRUCache(int capacity): capacity(capacity) {}
 4     
 5     int get(int key) {
 6         if(pos.find(key) != pos.end())
 7         {
 8             put(key, pos[key]->second);
 9             return pos[key]->second;
10         }
11         return -1;
12     }
13     
14     void put(int key, int value) {
15         if(pos.find(key) != pos.end())
16         {
17             recent.erase(pos[key]);
18         }
19         else
20         {
21             if(recent.size() >= capacity)
22             {
23                 pos.erase(recent.back().first);
24                 recent.pop_back();
25             }
26         }
27         recent.push_front({key, value});
28         pos[key] = recent.begin();
29     }
30 private:
31     int capacity;
32     list<pair<int, int>> recent;
33     unordered_map<int, list<pair<int, int>>::iterator> pos;
34 };
35 
36 /**
37  * Your LRUCache object will be instantiated and called as such:
38  * LRUCache* obj = new LRUCache(capacity);
39  * int param_1 = obj->get(key);
40  * obj->put(key,value);
41  */

 

Guess you like

Origin www.cnblogs.com/LJ-LJ/p/11204068.html