Least Recently Used(LRU) Cache

来源:互联网 发布:广电授权的网络机顶盒 编辑:程序博客网 时间:2024/04/20 05:01

According to LeetCode:

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 (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 should invalidate the least recently used item before inserting a new item.

When dealing with (key, vlaue) pairs, the most straight-forward data structure is hashmap(map or unordered_map in C++). However, at least for C++, it is difficult to control the insertion of new items: (1) the position you can specify is just a hint and does not force the new element to be inserted at that position within the map/unordered_map container, and (2) there is no push_back or push_front methods provided for map/unordered_map.

- See more at:http://bo-yang.github.io/2014/06/12/lru-cache/#sthash.Pj0d5XQ1.dpuf

http://bo-yang.github.io/2014/06/12/lru-cache

0 0
原创粉丝点击