LRU缓存机制
    题目链接:https://leetcode-cn.com/problems/lru-cache/
    图片.png
    思路:数据结构分为两个部分:数据被访问顺序的存储;数据的查询

    • 存储数据被访问顺序的部分涉及中间删除、两头增删操作,因此采用链表
    • 数据查询部分要求O(1),因此想到用哈希表

    综上,采用哈希表存储数据与链表迭代器的映射,链表存储数据的值,并维护被访问的顺序;当数据被访问或过期时,同步更新哈希表中的迭代器。

    参考代码:

    1. class LRUCache {
    2. private:
    3. unordered_map<int, list<pair<int, int>>::const_iterator> _map;
    4. list<pair<int, int>> _list;
    5. int maxSize;
    6. public:
    7. LRUCache(int capacity) {
    8. maxSize = capacity;
    9. }
    10. int get(int key) {
    11. unordered_map<int, list<pair<int, int>>::const_iterator>::const_iterator it = _map.find(key);
    12. if (it == _map.end()) {
    13. return -1;
    14. }
    15. int value = it->second->second;
    16. _list.erase(it->second);
    17. _list.push_front(make_pair(key, value));
    18. _map[key] = _list.cbegin();
    19. return value;
    20. }
    21. void put(int key, int value) {
    22. unordered_map<int, list<pair<int, int>>::const_iterator>::const_iterator it = _map.find(key);
    23. if (it != _map.end()) {
    24. _list.erase(it->second);
    25. _list.push_front(make_pair(key, value));
    26. _map[key] = _list.cbegin();
    27. }
    28. else {
    29. if (_map.size() < maxSize) {
    30. _list.push_front(make_pair(key, value));
    31. _map[key] = _list.cbegin();
    32. }
    33. else {
    34. list<pair<int, int>>::const_iterator end = _list.cend();
    35. --end;
    36. _map.erase(end->first);
    37. _list.pop_back();
    38. _list.push_front(make_pair(key, value));
    39. _map[key] = _list.cbegin();
    40. }
    41. }
    42. }
    43. };