LRU设计
list是双向链表,map保存key对应到list中的迭代器的位置,list保存<key,value>
class LRUCache{
public:
LRUCache(int capacity) {
c = capacity;
}
int get(int key) {
if (mymap.count(key)==0)
return -1;
list<pair<int, int>>::iterator it = mymap[key];
int val = it->second;
mylist.erase(it);
mylist.push_front(pair<int,int>(key,val));
mymap[key] = mylist.begin();
return val;
}
void set(int key, int value) {
if (mymap.count(key) != 0)
{
list<pair<int, int>>::iterator it = mymap[key];
mylist.erase(it);
mylist.push_front(pair<int, int>(key, value));
mymap[key] = mylist.begin();
}
else
{
if (mymap.size() == c)
{
mymap.erase(mylist.back().first);
mylist.pop_back();
}
mylist.push_front(pair<int, int>(key, value));
mymap[key] = mylist.begin();
}
}
private:
list<pair<int, int>> mylist;
map<int, list<pair<int,int>>::iterator> mymap;
int c;
};
LRU设计的更多相关文章
- 字节面试问我如何高效设计一个LRU,当场懵
首发公众号:bigsai 转载请放置作者和原文(本文)链接 前言 大家好,我是bigsai,好久不见,甚是想念! 最近有个小伙伴跟我诉苦,说他没面到LRU,他说他很久前知道有被问过LRU的但是心想自己 ...
- 转:为什么Uber宣布从Postgres切换到MySQL?
转: http://mp.weixin.qq.com/s?__biz=MzAwMDU1MTE1OQ==&mid=2653547609&idx=1&sn=cbb55ee823dd ...
- MySQL中的这个池子,强的一批!
Mysql 中数据是要落盘的,这点大家都知道.读写磁盘速度是很慢的,尤其和内存比起来更是没的说.但是,我们平时在执行 SQL 时,无论写操作还是读操作都能很快得到结果,并没有预想中的那么慢. 可能你会 ...
- LeetCode之LRU Cache 最近最少使用算法 缓存设计
设计并实现最近最久未使用(Least Recently Used)缓存. 题目描述: Design and implement a data structure for Least Recently ...
- 简单的LRU Cache设计与实现
要求: 设计并实现一个LRU缓存的数据结构,支持get和set操作 get(key):若缓存中存在key,返回对应的value,否则返回-1 set(key,value):若缓存中存在key,替换其v ...
- LRU算法的设计
一道LeetCode OJ上的题目,要求设计一个LRU(Least Recently Used)算法,题目描述如下: Design and implement a data structure for ...
- 面试挂在了 LRU 缓存算法设计上
好吧,有人可能觉得我标题党了,但我想告诉你们的是,前阵子面试确实挂在了 RLU 缓存算法的设计上了.当时做题的时候,自己想的太多了,感觉设计一个 LRU(Least recently used) 缓存 ...
- LeetCode:146_LRU cache | LRU缓存设计 | Hard
题目:LRU cache Design and implement a data structure for Least Recently Used (LRU) cache. It should su ...
- LeetCode题解: LRU Cache 缓存设计
LeetCode题解: LRU Cache 缓存设计 2014年12月10日 08:54:16 邴越 阅读数 1101更多 分类专栏: LeetCode 版权声明:本文为博主原创文章,遵循CC 4 ...
随机推荐
- 重写成员“log4net.Util.ReadOnlyPropertiesDictionary.GetObjectData(System.Runtime.Serialization.SerializationInfo, System.Runtime.Serialization.StreamingContext)”时违反了继承安全性规则
在.NET 4.0下使用最新版本的log4Net 1.2.10,会遇到下面这样的错误: 重写成员“log4net.Util.ReadOnlyPropertiesDictionary.GetObject ...
- 代码覆盖率工具 EMMA
使用 EMMA 获得功能测试覆盖率 测试覆盖率是评价测试完整性的重要的度量标准之一. EMMA 是一个面向 Java 代码的测试覆盖率收集工具.在测试过程中,使用 EMMA 能使收集和报告测试覆盖率的 ...
- codevs[1300]文件排版
Description 写电子邮件是有趣的,但不幸的是经常写不好看,主要是因为所有的行不一样长,你的上司想要发排版精美的电子邮件,你的任务是为他编写一个电子邮件排版程序. 完成这个任务最简单的办法是在 ...
- luogu1207双重回文数[usaco1.2]Dual Palindromes
题目描述 如果一个数从左往右读和从右往左读都是一样,那么这个数就叫做“回文数”.例如,12321就是一个回文数,而77778就不是.当然,回文数的首和尾都应是非零的,因此0220就不是回文数. 事实上 ...
- C# .net中cookie值为中文时的乱码解决方法
一.cookie的名称或子cookie的名称不能为中文,否则无法获得cookie 这个好办,名称不用中文即可 二.cookie的值为中文时候,取cookie的值会出现乱码 解决办法:存取cookie时 ...
- 【转】【MySQL】mysql 通过bin-log恢复数据方法详解
mysql中bin-log在mysql默认状态下是没有打开的,我们要先打开mysql 开启bin-log功能,然后再通过备份的bin-log进行数据库恢复了. 具体的操作是通过mysqlbinlog这 ...
- [转]终于找到全annotation配置springMVC的方法了(事务不失效)
原文:http://icanfly.iteye.com/blog/778401 icanfly 写道 如果带上事务,那么用annotation方式的事务注解和bean配置,事务会失效,要将servic ...
- scrapy 保存到 sqlite3
scrapy 爬取到结果后,将结果保存到 sqlite3,有两种方式 item Pipeline Feed Exporter 方式一 使用 item Pipeline 有三个步骤 文件 pipelin ...
- Android -- TouchDelegate
设计规定 Android4.0设计规定的有效可触摸的UI元素标准是48dp,这是一个用户手指能准确并且舒适触摸的区域. 如下图所示,你的UI元素可能小于48dp,图标仅有32dp,按钮仅有40dp,但 ...
- [MetaHook] Find a function signature
Find a non-public function signature, we need a tool "IDA Pro" ( You can open picture in a ...