• LeetCode-146. LRU Cache [C++][Java]


    LeetCode-146. LRU CacheLevel up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.https://leetcode.com/problems/lru-cache/

    Design a data structure that follows the constraints of a Least Recently Used (LRU) cache.

    Implement the LRUCache class:

    • LRUCache(int capacity) Initialize the LRU cache with positive size capacity.
    • int get(int key) Return the value of the key if the key exists, otherwise return -1.
    • void put(int key, int value) Update the value of the key if the key exists. Otherwise, add the key-value pair to the cache. If the number of keys exceeds the capacity from this operation, evict the least recently used key.

    The functions get and put must each run in O(1) average time complexity.

    Example 1:

    Input
    ["LRUCache", "put", "put", "get", "put", "get", "put", "get", "get", "get"]
    [[2], [1, 1], [2, 2], [1], [3, 3], [2], [4, 4], [1], [3], [4]]
    Output
    [null, null, null, 1, null, -1, null, -1, 3, 4]
    
    Explanation
    LRUCache lRUCache = new LRUCache(2);
    lRUCache.put(1, 1); // cache is {1=1}
    lRUCache.put(2, 2); // cache is {1=1, 2=2}
    lRUCache.get(1);    // return 1
    lRUCache.put(3, 3); // LRU key was 2, evicts key 2, cache is {1=1, 3=3}
    lRUCache.get(2);    // returns -1 (not found)
    lRUCache.put(4, 4); // LRU key was 1, evicts key 1, cache is {4=4, 3=3}
    lRUCache.get(1);    // return -1 (not found)
    lRUCache.get(3);    // return 3
    lRUCache.get(4);    // return 4

    【C++】

    1. class LRUCache {
    2. unordered_map<int, listint, int>>::iterator> hash;
    3. listint, int>> cache;
    4. int size;
    5. public:
    6. LRUCache(int capacity):size(capacity) {}
    7. int get(int key) {
    8. auto it = hash.find(key);
    9. if (it == hash.end()) {
    10. return -1;
    11. }
    12. cache.splice(cache.begin(), cache, it->second);
    13. return it->second->second;
    14. }
    15. void put(int key, int value) {
    16. auto it = hash.find(key);
    17. if (it != hash.end()) {
    18. it->second->second = value;
    19. return cache.splice(cache.begin(), cache, it->second);
    20. }
    21. cache.insert(cache.begin(), make_pair(key, value));
    22. hash[key] = cache.begin();
    23. if (cache.size() > size) {
    24. hash.erase(cache.back().first);
    25. cache.pop_back();
    26. }
    27. }
    28. };
    29. /**
    30. * Your LRUCache object will be instantiated and called as such:
    31. * LRUCache* obj = new LRUCache(capacity);
    32. * int param_1 = obj->get(key);
    33. * obj->put(key,value);
    34. */

    或者懒一点

    1. class LRUCache {
    2. private:
    3. int capacity;
    4. listint, int>> recent; //pair:key,value
    5. unordered_map<int, listint, int>>::iterator> pos; //key, iterator
    6. public:
    7. LRUCache(int capacity): capacity(capacity) {}
    8. int get(int key) {
    9. if(pos.find(key) != pos.end()) {
    10. int value = pos[key]->second;
    11. put(key, value);
    12. return value;
    13. }
    14. return -1;
    15. }
    16. void put(int key, int value) {
    17. if(pos.find(key) != pos.end()) recent.erase(pos[key]);
    18. else if (recent.size() >= capacity) {
    19. int old = recent.back().first;
    20. recent.pop_back();
    21. pos.erase(old);
    22. }
    23. recent.push_front(make_pair(key, value));
    24. pos[key] = recent.begin();
    25. }
    26. };
    27. /**
    28. * Your LRUCache object will be instantiated and called as such:
    29. * LRUCache* obj = new LRUCache(capacity);
    30. * int param_1 = obj->get(key);
    31. * obj->put(key,value);
    32. */

    【Java】

    1. class LRUCache {
    2. int cap;
    3. LinkedHashMap cache = new LinkedHashMap<>();
    4. public LRUCache(int capacity) {
    5. this.cap = capacity;
    6. }
    7. public int get(int key) {
    8. if(!cache.containsKey(key)){
    9. return -1;
    10. }
    11. makeRecently(key);
    12. return cache.get(key);
    13. }
    14. public void put(int key, int value) {
    15. if(cache.containsKey(key)){
    16. cache.put(key, value);
    17. makeRecently(key);
    18. return;
    19. }
    20. if(cache.size() >= this.cap){
    21. int oldestKey = cache.keySet().iterator().next();
    22. cache.remove(oldestKey);
    23. }
    24. cache.put(key, value);
    25. }
    26. private void makeRecently(int key){
    27. int val = cache.get(key);
    28. cache.remove(key);
    29. cache.put(key, val);
    30. }
    31. }
    32. /**
    33. * Your LRUCache object will be instantiated and called as such:
    34. * LRUCache obj = new LRUCache(capacity);
    35. * int param_1 = obj.get(key);
    36. * obj.put(key,value);
    37. */

    参考文献

    【1】常见缓存算法和LRU的c++实现_banyan3646的博客-CSDN博客

  • 相关阅读:
    KDChart3.0编译过程-使用QT5.15及QT6.x编译
    vue-tour新手指导,点击按钮,进行提示,再次点击按钮,提示隐藏,点击下一步,弹框显示
    基于java+swing+mysql北方传统民居信息管理系统
    C++代码重用(一)
    Django-ORM 单表查询
    sklearn中的SparseCoder是什么?又该如何使用
    Less基础速学 —— 混入、运算、继承
    uCharts常用图表组件demo
    SpringBoot - SpringBoot配置说明
    Unity 使用技巧与常见问题
  • 原文地址:https://blog.csdn.net/qq_15711195/article/details/126456263