Title Description
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 ('ll 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 is reached its capacity, it should invalidate the least recently used item before inserting a new item.
The topic asks us to implement a cached data structure. The primary action required is get (get) and insert (set). It is important to note that when the cache is full, you need to delete the element that is not used for the longest time in set and insert the new element. This requires that we design a method in the data structure to either record the order in which each element is used, or to preserve the elements in the order in which they are used. It is important to note that, in the get element, if the element exists in the cache, the order in which the element is to be used is advanced.
You can represent the data in the cache by using a doubly linked list. The head of the list represents the most recently used element, and the tail of the linked list represents the element that was not used for the longest time. When the element is fetched/inserted, the element is placed in the head of the linked list. It is important to note that if the key of the inserted element already exists in the cache, the original data (key, value) node is deleted.
We know that the time complexity of random access to a linked list is o ( n ) , the time complexity of each acquisition and insertion of an element in the above data structure is o ( n ) 。 In order to reduce the complexity of time, we can use a map structure, save the node location with key, the time complexity of < Span class= "Mrow" id= "mathjax-span-996" > o ( l o g n ) 。 If higher time efficiency is required, you can also use UNORDERED_MAP instead of map to save the node location.
The map structure is as follows:
map<int, node*> cache;
The complete code obtained from the above ideas is as follows:
structnode{intKeyintValue node* Pre; Node* Next; NodeintKintV): Key (k), Value (v), pre (NULL), Next (null) {};};classlrucache{Private:intm_capacity;intM_size; node* M_head; node* M_tail; map<int, node*>M_cache; Public: LRUCache (intcapacity) {m_capacity = capacity; M_size =0; M_head =NewNode0,0); M_tail =NewNode0,0); M_head->next = M_tail; M_tail->pre = M_head; M_cache.clear (); } ~lrucache () {DeleteM_head;DeleteM_tail; map<int, node*>:: Iterator it; for(it = M_cache.begin (); It! = M_cache.end (); ++it) {Delete(It->second); } m_cache.clear (); }voidPuttohead (node* cur) {cur->next = m_head->next; Cur->next->pre = cur; M_head->next = cur; Cur->pre = M_head; }intGetintKey) { map<int, node*>:: Iterator it = m_cache.find (key);if(It! = M_cache.end ()) {node* cur = it->second; Cur->pre->next = cur->next; Cur->next->pre = cur->pre; Puttohead (cur);returncur->value; }Else return-1; }void Set(intKeyintValue) { map<int, node*>:: Iterator it = m_cache.find (key);if(It! = M_cache.end ()) {node* cur = it->second; Cur->value = value; Cur->pre->next = cur->next; Cur->next->pre = cur->pre; Puttohead (It->second); }Else{node* New_node =NewNode (key, value); Puttohead (New_node); M_cache[key] = New_node;if(M_size < m_capacity) m_size++;Else{node* del = m_tail->pre; Del->pre->next = M_tail; M_tail->pre = del->pre; it = M_cache.find (Del->key); M_cache.erase (IT);DeleteDel } } }};
Leetcode (+) LRU Cache