Leetcode: LRU Cache

Source: Internet
Author: User

Leetcode: LRU Cache

Design and implement a data structure for least recently used (LRU) cache. It shocould support the following operations:getAndset.

 

get(key)-Get the value (will 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 reached its capacity, it shocould invalidate the least recently used item before inserting a new item.

Address: https://oj.leetcode.com/problems/lru-cache/

Algorithm: According to the question, let's simulate the LRU algorithm (the most commonly used algorithm recently ). A linked list is used to represent the cache. This facilitates the deletion of any elements in the linked list and the insertion of header node elements. In addition, a map is used to index each key value in the linked list, so that the corresponding position of the key value in the linked list can be quickly found. The algorithm implements three functions: the first is the constructor, which is used to set the cache capacity; the second get function, which is used to obtain the value corresponding to the key value. First, search for the cache, if the key value is not found,-1 is returned. If the key value is found, the elements corresponding to the key are moved to the head node of the linked list and the index value is updated; the third function is the set function, which is used to set the value corresponding to the key value. First, search for the cache. If the key value is found, set the value corresponding to the key value, move the key value to the head of the linked list and update the index value. If the index value is not found and the cache capacity has not reached the maximum value, insert the key value to the head of the linked list, insert the index value corresponding to the key value. If the index value is not found and the cache capacity has reached the maximum value, delete the last node of the linked list and delete its index value according to the LRU algorithm principle, insert the key value into the head of the linked list and the index value. Code:

 1 class LRUCache{ 2 public: 3     LRUCache(int capacity) { 4         cap = capacity; 5     } 6      7     int get(int key) { 8         map<int,Iter>::iterator it = index_map.find(key); 9         if(it == index_map.end()){10             return -1;11         }12         Iter p = it->second;13         int val = it->second->second;14         cache.push_front(*p);15         it->second = cache.begin();16         cache.erase(p);17         return val;18     }19     20     void set(int key, int value) {21         map<int,Iter>::iterator it = index_map.find(key);22         if (it != index_map.end()){23             cache.push_front(make_pair(key,value));24             cache.erase(it->second);25             it->second = cache.begin();26             return ;27         }28         if (cap > cache.size()){29             cache.push_front(make_pair(key,value));30             index_map[key] = cache.begin();31             return ;32         }33         index_map.erase(cache.back().first);34         cache.pop_back();35         cache.push_front(make_pair(key,value));36         index_map[key] = cache.begin();37     }38     typedef list<pair<int,int> >::iterator Iter;39     list<pair<int,int> > cache;40     map<int,Iter> index_map;41     int cap;42 };

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.