其实也米有很难……只是c++11的api这么好用的吗
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 (will 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 reached its capacity, it should invalidate the least recently used item before inserting a new item.
Follow up:
Could you 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 1 cache.put(3, 3); // evicts key 2 cache.get(2); // returns -1 (not found) cache.put(4, 4); // evicts key 1 cache.get(1); // returns -1 (not found) cache.get(3); // returns 3 cache.get(4); // returns 4
class LRUCache { public: LRUCache(int capacity) { size = capacity; } int get(int key) { auto it = hash.find(key); if (it != hash.end()){ // find // it->second移到cache.begin()前面的位置 cache.splice(cache.begin(), cache, it->second); return it->second->second; // return value } else return -1; } void put(int key, int value) { auto it = hash.find(key); if (it != hash.end()){ // find it->second->second = value; return cache.splice(cache.begin(), cache, it->second); } cache.insert(cache.begin(), make_pair(key, value)); hash[key] = cache.begin(); if (cache.size() > size){ hash.erase(cache.back().first); cache.pop_back(); } } private: // C++ STL中,哈希表对应的容器是 unordered_map(since C++ 11)。根据 C++ 11 标准的推荐,用 unordered_map 代替 hash_map. // STL中,map 对应的数据结构是 红黑树。红黑树是一种近似于平衡的二叉查找树,里面的数据是有序的。在红黑树上做查找操作的时间复杂度为 O(logN)。而 unordered_map 对应 哈希表,哈希表的特点就是查找效率高,时间复杂度为常数级别 O(1), 而额外空间复杂度则要高出许多。所以对于需要高效率查询的情况,使用 unordered_map 容器。而如果对内存大小比较敏感或者数据存储要求有序的话,则可以用 map 容器。 // Lists将元素按顺序储存在链表中. 与 向量(vectors)相比, 它允许快速的插入和删除,但是随机访问却比较慢. // hash表:存储key //unordered_map<int, list<pair(int, int)>::iterator> hash; unordered_map<int, list<pair<int, int>>::iterator> hash; // 链表:缓存区 list<pair<int, int>> cache; // 缓存区大小 int size; }; /** * Your LRUCache object will be instantiated and called as such: * LRUCache* obj = new LRUCache(capacity); * int param_1 = obj->get(key); * obj->put(key,value); */
原文地址:https://www.cnblogs.com/sherry-yang/p/10807406.html
时间: 2024-10-24 03:01:15