首页 > 数据库 >[Oracle] LeetCode 146 LRU Cache 经典题

[Oracle] LeetCode 146 LRU Cache 经典题

时间:2022-09-30 03:44:16浏览次数:46  
标签:146 Cache int cache value LRU key lru LRUCache

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.

Solution

我们用 \(list\) 套 \(pair\) 来表示 \(LRU\), 这样我们可以方便的进行插入和删除。

对于 \(cache\), 我们用 \(unordered\_map\) 来表示,映射 \(key\) 到 \(list[pair]\).

点击查看代码
class LRUCache {
private:
    list<pair<int,int>> lru;
    unordered_map<int,list<pair<int,int>>::iterator> cache;
    int cap;
    
public:
    LRUCache(int capacity) {
        this->cap = capacity;
    }
    
    int get(int key) {
        if(cache.find(key)==cache.end())return -1;
        auto ele = cache[key];
        int val = (*ele).second;
        lru.erase(ele);
        cache[key] = lru.insert(lru.end(), {key,val});
        return val;
    }
    
    void put(int key, int value) {
        if(cache.find(key)==cache.end()){
            if(cap==lru.size()){
                cache.erase(lru.front().first);
                lru.pop_front();
            }
        }
        else{
            lru.erase(cache[key]);
        }
        cache[key]=lru.insert(lru.end(),{key,value});
    }
};

/**
 * 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);
 */

标签:146,Cache,int,cache,value,LRU,key,lru,LRUCache
From: https://www.cnblogs.com/xinyu04/p/16743634.html

相关文章

  • Javascript 手写 LRU 算法
    LRU是LeastRecentlyUsed的缩写,即最近最少使用。作为一种经典的缓存策略,它的基本思想是长期不被使用的数据,在未来被用到的几率也不大,所以当新的数据进来时我们可以优先......
  • Cache缓存帮助类
    publicclassCache{privatestaticCache_cache=HttpRuntime.Cache;///<summary>///本地缓存获取///</summary>///<paramname="name"......
  • ARC146C Even XOR(线性基,组合)
    ARC146CEvenXOR有多少集合\(S\),每个元素都在\([0,2^N)\)之间,且所有偶数大小的子集的异或和不为\(0\)。CODE奇数大小的子集\(\oplus\)和可以为\(0\),可是如果......
  • 视频融合云平台EasyCVR级联时出现报错“Error 1146",是什么原因?
    EasyCVR具备强大的视频接入、汇聚与管理、视频分发、设备管理、用户及角色权限管理等能力。平台可提供的丰富的视频功能,包括:视频监控直播、云端录像、云存储、录像检索与回......
  • Memcached vs Redis, 挑选哪一个?
    MemcachedvsRedis,挑选哪一个?标签:MencachedRedisMemchached还是Redis?该用哪一个?当我们讨论改进性能的时候,这是每次技术讨论中最常见的一个问题。每当性能需要改善时......
  • 安装redis-4.0.9和memcached
    安装redis-4.0.9和memcached yum-yinstallgcc-c++wgetvimunzipzipmemcachedcd/mkdirsoftwarecd/softwarewgethttp://download.redis.io/releases/redis-4.0......
  • memcached安装
    安装memcached yuminstall-ymemcached /usr/bin/memcached-d-l192.168.7.129 -p11211-m150-uroot -d守护进程模式(退出终端窗口之后使程序还在运行),-l......
  • springboot 整合Ehcache的使用
    Springboot提供了换粗的统一整合接口,方便缓存技术的开发与管理。Generic,JCache,Ehcache,Hazelcast,Infinispan,Couchbase,Redis,Caffenine,Simple(默认缓存),Memcached。如何整合......
  • 整合第三方缓存EHCache
    a>添加依赖<!--MybatisEHCache整合包--><dependency><groupId>org.mybatis.caches</groupId><artifactId>mybatis-ehcache</artifactId><version>1.2.1</version>......
  • LRU
    //LRU缓存机制//使用哈希表+双向链表(维护使用的频率)classLRUCache{classDoubleLinkedNode{intkey;intvalue;DoubleLink......