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. JAVA设计模式详解(三)----------装饰者模式

    今天LZ带给大家的是装饰者模式,提起这个设计模式,LZ心里一阵激动,这是LZ学习JAVA以来接触的第一个设计模式,也许也是各位接触的第一个设计模式.记得当初老师在讲IO的时候就提到过它:“是你还有你, ...

  2. nginx命令(持续更新)

    关闭服务:nginx -s stop | service nginx stop 启动服务:nginx | service nginx start 重新加载配置文件:nginx -s reload |  ...

  3. js-ES6学习笔记-Proxy(2)

    1.has方法用来拦截HasProperty操作,即判断对象是否具有某个属性时,这个方法会生效.典型的操作就是in运算符. var handler = { has (target, key) { if ...

  4. C# Newtonsoft.Json反序列化为dynamic对象之后的使用

    通过Newtonsoft.Json将一个json类型的字符串反序列化为dynamic后直接使用报错 源代码: namespace ConsoleApplication1 { class Program ...

  5. 让 Angular 应用动起来!

    [编者按]本文主要通过生动的实例,介绍为 Angular 应用添加动画的原理与过程.文章系国内 ITOM 管理平台 OneAPM 编译呈现. 我们知道,Angular 应用在更新 DOM 时,会直接将 ...

  6. 重学C语言---02C语言概述

    1.第一个C语言实例 #include<stdio.h> int main(void) /*一个简单的C程序*/ { int num; /*定义一个num的变量*/ num = ; /*为 ...

  7. python基础一数据类型之字典

    摘要: python基础一数据类型之一字典,这篇主要讲字典. 1,定义字典 2,字典的基础知识 3,字典的方法 1,定义字典 1,定义1个空字典 dict1 = {} 2,定义字典 dict1 = d ...

  8. Azure 虚拟机诊断设置问题排查

    Azure 为用户提供了可以自己配置的性能监控功能:Azure 诊断扩展.但是在具体配置中,经常会遇到各种各样的问题.不了解监控的工作机制常常给排查带来一定难度.这里我们整理了关于 Azure 虚拟机 ...

  9. 03-04_配置并启动Managed Server(受管服务器)

    本文重点: 配置Managed Servers(受管服务器) 启动Managed Servers 原理 运行多个Managed Servers实例             一.配置Managed Se ...

  10. Oracle EBS AR 客户API

    ------------------------------------ 1. Set Environment ------------------------------------ -- 1a. ...