146. LRU Cache

Source: Internet
Author: User

146. LRU Cache
    • Total accepted:109086
    • Total submissions:675208
    • Difficulty:hard
    • Contributors:admin

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 ('ll 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 is reached its capacity, it should invalidate the least recently used item before inserting a new item.

Follow up:
Could 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 1cache.put (3, 3);    Evicts key 2cache.get (2);       Returns-1 (not found) Cache.put (4, 4);    Evicts key 1cache.get (1);       Returns-1 (not found) Cache.get (3);       Returns 3cache.get (4);       Returns 4

We need the data structures:

  list: List<pair<int, Int>>//record the Order of objects

  hashmap: Map<int, List<pair<int, int>>::iterator>//mapping the relationship between key and Objects

Must use the iterator, otherwise the splice function won't work

Functions:

Set (key, value)//if the object exists int the map, erase it in the list first, and then push the object from the front. Set M[key] = L.front ();

If the size of map is larger than capacity, erase the object both from list and map;

Get (key)//if the objects do not exist in map, return-1;

The only change was the order in the list.

To_list.splice (position, from_list, iterator)

1 classLRUCache {2  Public:3LRUCache (intcapacity) {4Cap =capacity;5     }6     7     int Get(intkey) {8Auto it =M.find (key);9         if(It = = M.end ())return-1;//Not findTenL.splice (L.begin (), L, it-second);//move it in L to L.begin (); One         returnIt-secondsecond; A     } -      -     voidPutintKeyintvalue) { theAuto it =M.find (key); -         if(It! = M.end ()) L.erase (IT-second); -         //L.push_front (IT-second); - L.push_front (Make_pair (key, value)); +M[key] =L.begin (); -         if(M.size () >cap) { +Auto k = L.rbegin (), first;//to save int type or pointer unsafe because the list to be pop_back later A L.pop_back (); at             //M.erase (k-first); - M.erase (k); -         } -     } - Private: -     intcap; inlist<pair<int,int>> l;//record the order of least recently used -map<int, list<pair<int,int>>::iterator> m;//HashMap must be iterator . to }; +  - /** the * Your LRUCache object would be instantiated and called as such: * * LRUCache obj = new LRUCache (capacity); $ * int param_1 = Obj.get (key);Panax Notoginseng * Obj.put (key,value); -  */
View Code

146. LRU Cache

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.