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. Mac下,(OS系统)IDEA 逆向工程,生成 hibernate 映射文件以及对应的javaBean(类似于eclipse)

    找了好久,逆向工程生成javabean的,在Macbook的OS系统下使用idea生成. 参考博客:http://m.blog.csdn.net/quan20111992/article/detail ...

  2. CentOS总结归纳之基本操作(linux系管与运维一)

    原创作品,转载请在文章明显位置注明出处:https://www.cnblogs.com/sunshine5683/p/10170009.html 使用命令关闭和重启系统: 一.条件:只有root用户才 ...

  3. Binary Tree Traversals(HDU1710)二叉树的简单应用

    Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  4. Fragment的坑

    http://www.jianshu.com/p/d9143a92ad94 使用add()加入fragment时将触发onAttach(),使用attach()不会触发onAttach() 使用rep ...

  5. java的XML解析(DOM4J技术)

    DOM4J技术解析XML文件 一,XML简介 xml (可扩展标记语言)  全称: Extended Markup Language 可扩展的含义:允许程序员按照自己的想法去扩展新的标签 注意:但是扩 ...

  6. 走通Django的基本流程

    工程目录及文件的说明 manage.py:一个命令行工具,可以使我们用多种方式对Django项目进行交互 __init__.py:一个空文件,它告诉Python这个文件的上级目录应该看做一个pytho ...

  7. requireJS基本概念及使用流程(2)

    上一篇我们一起研究了研究requireJS,这一篇我们来说一说requireJS具体的使用过程 其实很简单的,我总结了总结就是分为四步走 第一步:在页面中引入requireJS并且引入入口文件 第二步 ...

  8. js 函数中形参与实参的关系

    函数中形参与实参的关系 对于形参和实参的定义,在 权威指南中有着明确的定义.但是,我们更在意的是它们之间的关系,到底形参会不会影响到实参? 形参到底会不会影响到实参? 对于这个问题的答案,请先看以下两 ...

  9. Oracle查询时15分钟划分

    select to_date(to_char(sysdate, 'yyyy-MM-dd hh24') || ':' ||               floor(to_number(to_char(s ...

  10. Maven学习(一)概念简述和安装教程

    刚开始由.net转向java,公司的项目中就开始使用maven,感觉他就像nuget一样,但是他又比nuget要强大(毕竟他是项目管理工具). maven概述 在这里,就不背书包了,怎么简单直接怎么来 ...