Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set.

get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.
set(key, value) - Set or insert the value if the key is not already present. When the cache reached its capacity, it should invalidate the least recently used item before inserting a new item.

最近使用缓存的问题,看到key和value感觉可能就要使用map了。用一个list维护一个cache,然后map指向相应cache的每个节点。

注意这里的最近使用,不管是get还是set都属于最近使用的范畴,代码如下所示:

 class LRUCache{
struct cacheNode{
int key;
int val;
cacheNode(int k, int v):
key(k), val(v){}
};
public:
LRUCache(int capacity) {
size = capacity;
} int get(int key) {
if(cacheMap.find(key) != cacheMap.end()){
auto it = cacheMap[key];
cacheList.splice(cacheList.begin(), cacheList, it);
cacheMap[key] = cacheList.begin();
return cacheList.begin()->val;
}else
return -;
} void set(int key, int value) {
if(cacheMap.find(key) != cacheMap.end()){//list中存在该元素
auto it = cacheMap[key];
cacheList.splice(cacheList.begin(), cacheList, it);
cacheMap[key] == cacheList.begin();
cacheList.begin()->val = value;
}else{ //list中不存在该元素
if(cacheList.size() == this->size){
cacheMap.erase(cacheList.back().key);
cacheList.pop_back();
}
cacheList.push_front(cacheNode(key, value));
cacheMap[key] = cacheList.begin();
}
}
private:
int size;
list<cacheNode> cacheList;
unordered_map<int , list<cacheNode>::iterator> cacheMap;
};

LeetCode OJ:LRU Cache(最近使用缓存)的更多相关文章

  1. LeetCode之LRU Cache 最近最少使用算法 缓存设计

    设计并实现最近最久未使用(Least Recently Used)缓存. 题目描述: Design and implement a data structure for Least Recently ...

  2. [LeetCode] 146. LRU Cache 最近最少使用页面置换缓存器

    Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...

  3. [LeetCode] 146. LRU Cache 近期最少使用缓存

    Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...

  4. Java for LeetCode 146 LRU Cache 【HARD】

    Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...

  5. 【LeetCode】LRU Cache 解决报告

    插话:只写了几个连续的博客,博客排名不再是实际"远在千里之外"该.我们已经进入2一万内. 再接再厉.油! Design and implement a data structure ...

  6. leetcode@ [146] LRU Cache (TreeMap)

    https://leetcode.com/problems/lru-cache/ Design and implement a data structure for Least Recently Us ...

  7. 【leetcode】LRU Cache

    题目简述: Design and implement a data structure for Least Recently Used (LRU) cache. It should support t ...

  8. 【leetcode】LRU Cache(hard)★

    Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...

  9. 【Leetcode】 LRU Cache实现

    Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...

  10. leetcode 146. LRU Cache ----- java

    esign and implement a data structure for Least Recently Used (LRU) cache. It should support the foll ...

随机推荐

  1. Spring MVC 复习笔记03

    1. @RequestMapping 1). url映射 定义controller方法对应的url,进行处理器映射使用. 2). 窄化请求映射  3). 限制http请求方法 出于安全性考虑,对htt ...

  2. 高通camera结构(摄像头基础介绍)

    摄像头基础介绍 一.摄像头结构和工作原理. 拍摄景物通过镜头,将生成的光学图像投射到传感器上,然后光学图像被转换成电信号,电信号再经过模数转换变为数字信号,数字信号经过DSP加工处理,再被送到电脑中进 ...

  3. 【Python】IO编程

    文件读写 StringIO和BytesIO 操作文件和目录 序列化 学习廖老师的py官网的笔记 1.stream的概念.数据交换通常需要建立两根“水管”. 2.同步IO和异步IO.异步性能高,但是编程 ...

  4. php 安装Memcache扩展

    转载地址:http://www.tuicool.com/articles/EB3imm 文章概述:由于当前机器安装的php,是用yum安装,现在需要使用到memadmin做一些监控, memadmin ...

  5. CSS Border(边框)

    CSS Border(边框) 一.CSS 边框属性 CSS边框属性允许你指定一个元素边框的样式和颜色. 示例效果: 二.边框样式 边框样式属性指定要显示什么样的边界. border-style属性用来 ...

  6. Object的各种方法

    Object的一些知识点总结 1.hasOwnProperty obj.hasOwnProperty(prop) 参数 prop: 要检测的属性字符串名称或者Symbol 返回值 用来判断一个对象是否 ...

  7. hadoop https配置

    在 hadoop1生成ca并拷贝至hadoop2,hadoop2. (密码随便设置,大于6位即可.如adminadmin) cd /etc/https openssl req -new -x509 - ...

  8. 【vim】几种模式的切换

    很多初学者启动vim后,不知道怎么输入字符:按了半天字母,结果屏幕还是空的. vim和记事本或WORD不一样,不是一打开后就可以输入文字,此时它处于正常模式. vim一共有4个模式: 正常模式 (No ...

  9. node实现rar格式压缩

    背景 由于公司的CMS系统里,只接受rar格式压缩的文件,所以没法直接使用nodejs里提供的zip压缩组件.只能从winRar软件入手了,但网上没有多少这方面相关的东西,所以下面也是自己尝试着在做. ...

  10. python安装包下载慢的问题 | Python

    复制链接,打开迅雷就开始下载了.