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 ...
随机推荐
- NYOJ-取石子
(一) 描述一天,TT在寝室闲着无聊,和同寝的人玩起了取石子游戏,而由于条件有限,他/她们是用旺仔小馒头当作石子.游戏的规则是这样的.设有一堆石子,数量为N(1<=N<=1000000), ...
- python画柱状图并且输出到html文件
import matplotlibmatplotlib.use('Agg')import matplotlib.pyplot as pltfrom Cstring import StringIO y ...
- iOS 推送所调用的函数详解
AppDelegate类中: - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDic ...
- JS中NULL和Undefined的区别
NULL是表示一个”无“的对象,转换成数值为0:undefined是一个“无”的原始值,转为数值为NaN: 当声明的变量还未被初始化时,变量的默认值为undefined: null用来表示尚未存在的对 ...
- web框架学习列表
转载自鲁塔弗的博客,原文网址:http://lutaf.com/148.htm web framework层出不穷,特别是ruby/python,各有10+个,php/java也是一大堆 根据我自己的 ...
- xpath 参考
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Te ...
- 花生壳动态IP域名解析之python自动提交公网IP
#!/usr/bin/env python import re import os import time import random ip_current = '' while True: myip ...
- Windows Phone App Studio 无码开发手机应用
上周微软发布了一款基于Web的Windows Phone应用开发工具 "Windows Phone App Studio".它与大家熟知Visual Studio的最大不同之处是W ...
- NET Core中怎么使用HttpContext.Current
NET Core中怎么使用HttpContext.Current 阅读目录 一.前言 二.IHttpContextAccessor 三.HttpContextAccessor 回到目录 一.前言 我们 ...
- 为easyui datagrid 添加上下方向键移动
将以下脚本保存为 easyui-datagrid-moverow.js var DatagridMoveRow = (function($){ function DatagridMoveRow(gri ...