hash:存储的key、value、freq

freq:存储的freq、key,也就是说出现1次的所有key在一起,用list连接

class LFUCache {
public:
    LFUCache(int capacity) {
        cap = capacity;
    }

    int get(int key) {
        auto it = hash.find(key);
        if(it == hash.end())
            ;                              这段代码以下处理的都是已有key的情况

        freq[hash[key].second].erase(iter[key]);    如果这个key已经有了,那次数就要加1。所以必须把freq里面的对应次数的key先删除,然后更新hash里key对应的次数,
        ++hash[key].second;                         同时在freq中将这个key连接次数加1的地方
        freq[hash[key].second].push_back(key);
        iter[key] = --freq[hash[key].second].end(); 因为这个key在freq中变换了位置,那对应的迭代器地址也应该改变,是push_back进去的,所以尾地址-1就好了
        )              代码走到这一步,一定是有key存在的。如果min_freq没有了值,证明就是
            ++min_freq;
        return hash[key].first;
    }

    void put(int key, int value) {
        )
            return;
        ){                          注意调用的是 get(key),不是find
            hash[key].first = value;                 如果这个key已经存在,就不用考虑容量的问题,直接更新value就好了,因为不用新添加
            return;
        }
        if(hash.size() >= cap){
            hash.erase(freq[min_freq].front());      删除出现次数最少的中最久未出现的,list中越新出现的从后push进去
            iter.erase(freq[min_freq].front());      为什么要删除迭代器???
            freq[min_freq].pop_front();
        }
        hash[key] = {value,};                       对出现一次的key进行添加
        freq[].push_back(key);
        iter[key] = --freq[].end();
        min_freq = ;
    }
    int cap,min_freq;
    unordered_map<int,pair<int,int>> hash;           <key,pair<value,freq>>
    unordered_map<int,list<int>> freq;         <freq,list<key>>
    unordered_map<int,list<int>::iterator> iter;     <freq,list<key>::iterator>
};

https://www.cnblogs.com/grandyang/p/6258459.html

leetcode 460. LFU Cache的更多相关文章

  1. [LeetCode] 460. LFU Cache 最近最不常用页面置换缓存器

    Design and implement a data structure for Least Frequently Used (LFU) cache. It should support the f ...

  2. leetcode 146. LRU Cache 、460. LFU Cache

    LRU算法是首先淘汰最长时间未被使用的页面,而LFU是先淘汰一定时间内被访问次数最少的页面,如果存在使用频度相同的多个项目,则移除最近最少使用(Least Recently Used)的项目. LFU ...

  3. [LeetCode]460.LFU缓存机制

    设计并实现最不经常使用(LFU)缓存的数据结构.它应该支持以下操作:get 和 put. get(key) - 如果键存在于缓存中,则获取键的值(总是正数),否则返回 -1.put(key, valu ...

  4. [LeetCode] LFU Cache 最近最不常用页面置换缓存器

    Design and implement a data structure for Least Frequently Used (LFU) cache. It should support the f ...

  5. Leetcode: LFU Cache && Summary of various Sets: HashSet, TreeSet, LinkedHashSet

    Design and implement a data structure for Least Frequently Used (LFU) cache. It should support the f ...

  6. LeetCode LFU Cache

    原题链接在这里:https://leetcode.com/problems/lfu-cache/?tab=Description 题目: Design and implement a data str ...

  7. [LeetCode] 146. LRU Cache 最近最少使用页面置换缓存器

    Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...

  8. LFU Cache

    2018-11-06 20:06:04 LFU(Least Frequently Used)算法根据数据的历史访问频率来淘汰数据,其核心思想是“如果数据过去被访问多次,那么将来被访问的频率也更高”. ...

  9. Leetcode:LRU Cache,LFU Cache

    在Leetcode上遇到了两个有趣的题目,分别是利用LRU和LFU算法实现两个缓存.缓存支持和字典一样的get和put操作,且要求两个操作的时间复杂度均为O(1). 首先说一下如何在O(1)时间复杂度 ...

随机推荐

  1. HDU3592(差分约束)

    World Exhibition Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  2. HDU4845(SummerTrainingDay02-C 状态压缩bfs)

    拯救大兵瑞恩 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Subm ...

  3. eclipse 中springboot2.0整合jsp 出现No Java compiler available for configuration options compilerClassName

    今天使用eclipse创建springboot整合jsp出现一个问题,在idea中并没有遇到这个问题.最后发现是需要在eclipse中添加一个eclipse依赖,依赖如下: <dependenc ...

  4. jquery 下拉框插件,实现智能补全,模糊搜索,多选

    近期已朋友问我问题,实现类似淘宝百度的下啦搜索条,看了网上好多帖子,都看起来好复杂,而且引用了好多没用的东西,而且多选选择内容多之后容易样式奔溃, 无奈之下只好自己改了, 话不多说上效果图: 模糊搜索 ...

  5. FI配置步骤清单

    1.定义公司代码   配置路径: R/3定制IMG的实施指南>企业结构>定义>财务会计>定义, 复制, 删除, 检查公司代码 事务代码 EC01 2.编辑科目表清单   配置路 ...

  6. JavaScript写计算器

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  7. Android逆向 编写一个Android程序

    本节使用的Android Studio版本是3.0.1 首先,我们先编写一个apk,后面用这个apk来进行逆向.用Android Studio创建一个新的Android项目,命名为Jhm,一路Next ...

  8. LeetCode题解之 Binary Tree Preorder Traversal

    1.题目描述 2.问题分析 利用递归. 3.代码 vector<int> preorderTraversal(TreeNode* root) { vector<int> v; ...

  9. Oracle EBS PO rcv_shipment_headers 数据缺失

    Datafix : How to Recreate Missing Receipt or Shipment Header Records (RCV_SHIPMENT_HEADERS table) (D ...

  10. SQL删除多列语句

    最近在写SQL过程中发现需要对一张表结构作调整(此处是SQL Server),其中需要删除多列,由于之前都是一条SQL语句删除一列,于是猜想是否可以一条语句同时删除多列,如果可以,怎么写法? 第一次猜 ...