码迷,mamicode.com
首页 > 系统相关 > 详细

Lintcode24 LFU Cache solution 题解

时间:2017-05-05 15:29:51      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:solution   recently   capacity   

【题目描述】

LFU (Least Frequently Used) is a famous cache eviction algorithm.For a cache with capacity k, if the cache is full and need to evict a key in it, the key with the lease frequently used will be kicked out.Implement set and get method for LFU cache.

LFU是一个著名的缓存算法。实现LFU中的set 和 get

【题目链接】

http://www.lintcode.com/en/problem/lfu-cache/

【题目解析】

这道题让我们实现一个LRU缓存器,LRU是Least Recently Used的简写,就是最近最少使用的意思。那么这个缓存器主要有两个成员函数,get和set,其中get函数是通过输入key来获得value,如果成功获得后,这对(key, value)升至缓存器中最常用的位置(顶部),如果key不存在,则返回-1。而set函数是插入一对新的(key, value),如果原缓存器中有该key,则需要先删除掉原有的,将新的插入到缓存器的顶部。如果不存在,则直接插入到顶部。若加入新的值后缓存器超过了容量,则需要删掉一个最不常用的值,也就是底部的值。具体实现时我们需要三个私有变量,cap, l和m,其中cap是缓存器的容量大小,l是保存缓存器内容的列表,m是哈希表,保存关键值key和缓存器各项的迭代器之间映射,方便我们以O(1)的时间内找到目标项。

【参考答案】

http://www.jiuzhang.com/solutions/lfu-cache/



Lintcode24 LFU Cache solution 题解

标签:solution   recently   capacity   

原文地址:http://12799252.blog.51cto.com/12789252/1922216

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!