Leetcode OJ LRU Cache (LRU caching)

Source: Internet
Author: User

Topic:

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.

Design data structures for the most recent time not using cache (LRU) to support get and set operations.

Get (key)-the key that is given a value for the keys. Returns the value of a key if it exists in the cache. otherwise returns-1.

Set (key, value)-resets or inserts the key value (if the key value does not exist). If the cache reaches its capacity, remove the most recent unused key value before inserting a new key value.

Ideas:

1, the use of linked list to save Key-value pairs, the linked list header represents the latest access or inserted key-value, the end of the list represents the longest unused.

If get a key or set a key-value, then move the corresponding key-value from the middle of the list to the list header indicates recently used, the longest unused key-value naturally fell to the tail of the list.

Use the list-specific splice function for this operation.

The splice function is described as follows:

List::splice realizes the function of list stitching. Deletes the contents or all elements of the source list and inserts them into the destination list.

Functions have the following three types of declarations:

void Splice (iterator position,list<t,allocator>& x); void Splice (iterator position,list<t,allocator> & X, iterator i), void splice (iterator position, list<t,allocator>&x, iterator first, iterator last);

Before moving the elements of the list x to the specified position in the destination list position, efficiently insert them into the destination list and Remove from x

The size of the destination list increases, increasing the size of the inserted element. The size of X will decrease the same size accordingly.


2, then use a hash table to save the location of Key-value.

Code:

Class Lrucache{public:lrucache () {} LRUCache (int capacity): capacity (capacity) {} int get (int key) {        if (cache.find (key) = = Cache.end ()) return-1; Items.splice (Items.begin (), items, Cache[key]);    Update the location of the access node and place it on the list head return cache[key]->second; } void set (int key, int value) {if (Cache.find (key) = = Cache.end ()) {///If the value to be inserted is not in the cache, the   Insert it into the list header and update the iterator in the cache if (capacity = = Cache.size ()) {int tmp = Items.back (). First;   Get Items.pop_back () at the end of the table;   Delete Footer Cache.erase (TMP) in the linked list;   Delete footer in cache} items.push_front (Make_pair (key, value));                 Insert the new value into the list header Cache[key] = Items.begin ();     Add new value to cache} else {cache[key]->second = value;     If a key already exists, update its value items.splice (Items.begin (), items, Cache[key]); Update the location of the node, placed in the list header}}private:list<Pair<int, int> >items; Key-value linked list, the table header is the latest node, the footer is the longest unused node, when there is a new node access or insertion, put to the table head Unordered_map<int, List<pair<int, int>;:: Iterator >cache; The key represents Key,value is the position of the Key-value node int capacity;};


Copyright NOTICE: Reprint please indicate the source.

Leetcode OJ LRU Cache (LRU caching)

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.