其實也米有很難……只是c++11的api這麼好用的嗎c++
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get
and put
.api
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:spa
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); */