• LeetCode LRU Cache


     1 class LRUCache{
     2 private:
     3     int max_size;
     4     unordered_map<int, list<pair<int, int> >::iterator> hash;
     5     list<pair<int, int> > lru_stack;
     6 public:
     7     LRUCache(int capacity) {
     8         if (capacity < 1) capacity = 0;
     9         max_size = capacity;
    10     }
    11 
    12     int get(int key) {
    13         unordered_map<int, list<pair<int, int> >::iterator>::iterator miter = hash.find(key);
    14         list<pair<int, int> >::iterator liter;
    15         if (miter == hash.end()) {
    16             return -1;
    17         } else {
    18             liter = miter->second;
    19             int val = liter->second;
    20             lru_stack.erase(liter);
    21             miter->second = liter = lru_stack.insert(lru_stack.begin(), make_pair(key, val));
    22             return liter->second;
    23         }
    24     }
    25 
    26     void set(int key, int value) {
    27         if (max_size == 0) return;
    28         unordered_map<int, list<pair<int, int> >::iterator>::iterator miter = hash.find(key);
    29         list<pair<int, int> >::iterator liter;
    30         if (miter == hash.end()) {   // key not exists in the cache
    31             if (hash.size() == max_size) {
    32                 // invalidate LRU item to avoid overflow
    33                 hash.erase(lru_stack.back().first);
    34                 lru_stack.pop_back();
    35             }
    36             liter = lru_stack.insert(lru_stack.begin(), make_pair(key, value));
    37             hash.insert(make_pair(key, liter));
    38         } else {    // key already exists, so replace the value with new one
    39             liter = miter->second;
    40             lru_stack.erase(liter);
    41             miter->second = lru_stack.insert(lru_stack.begin(), make_pair(key, value));
    42         }
    43     }
    44 };

    用一个链表来维护LRU关系,map来做hash,用了std库,如果纯粹自己手工写可能会花多点时间

    第二轮:

    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.

    最近在看Java集合发现LinkedHashMap自带LRU模式即使用LinkedHashMap(initialCapacity, loadFactor, accessOrder)构造函数指定accessOrder = true即使用访问顺序。不过本质上还是使用了hashmap加链表的形式来实现LRU过程。

    import java.util.*;
    public class LRUCache {
        private Map<Integer, Integer> storage;
        private int capacity;
        public LRUCache(int capacity) {
            storage = new LinkedHashMap<Integer, Integer>(capacity, 0.75f, true);
        
            this.capacity = capacity;
        }
        
        public int get(int key) {
            Integer value = storage.get(key);
            if (value == null) {
                return -1;
            }
            return value;
        }
        
        public void set(int key, int value) {
            boolean hasin = storage.keySet().contains(key);
            
            if (!hasin && capacity <= storage.size()) {
                Iterator<Map.Entry<Integer, Integer> > iter = storage.entrySet().iterator();
                Map.Entry<Integer, Integer> first = iter.next();
                storage.remove(first.getKey());
            }
            storage.put(key, value);
        }
    }
  • 相关阅读:
    Ubuntu修改源
    设置Div多行文本超出时,以省略号代替
    通过修改 Apache 的配置文件 htaccess 文件实现自定义404页面
    图片压缩的几种工具(初稿)
    DFA算法 处理屏蔽词库
    Xcode版本太低引发的bug,xcode各种版本下载方式详解
    Cocos2dx3.11.1Android播放视频,后台 黑屏,无法记忆播放bug修改
    cocos2d-x视频控件VideoPlayer的用户操作栏进度条去除(转载)
    python操作文件案例二则
    Live2d-cocos2dx教程(一)例子搭建及运行
  • 原文地址:https://www.cnblogs.com/lailailai/p/3641789.html
Copyright © 2020-2023  润新知