题目:

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

get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.
put(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.

The cache is initialized with a positive capacity.

Follow up:
Could you do both operations in O(1) time complexity?

Example:

LRUCache cache = new LRUCache( 2 /* capacity */ );

cache.put(1, 1);
cache.put(2, 2);
cache.get(1); // returns 1
cache.put(3, 3); // evicts key 2
cache.get(2); // returns -1 (not found)
cache.put(4, 4); // evicts key 1
cache.get(1); // returns -1 (not found)
cache.get(3); // returns 3
cache.get(4); // returns 4

分析:

设计和实现一个  LRU (最近最少使用) 缓存机制。它应该支持以下操作: 获取数据 get 和 写入数据 put 。

获取数据 get(key) - 如果密钥 (key) 存在于缓存中,则获取密钥的值(总是正数),否则返回 -1。
写入数据 put(key, value) - 如果密钥不存在,则写入其数据值。当缓存容量达到上限时,它应该在写入新数据之前删除最近最少使用的数据值,从而为新的数据值留出空间。

按照所给的例子看一下cache内部是如何变化的。

  front back result
put(1, 1) (1, 1)    
put(2, 2) (2, 2) (1, 1)  
get(1) (1, 1) (2, 2) 1
put(3, 3) (3, 3) (1, 1)  
get(2) (3, 3) (1, 1) -1
put(4, 4) (4, 4) (3, 3)  
get(1) (4, 4) (3, 3) -1
get(3) (3, 3) (4, 4) 3
get(4) (4, 4) (3, 3) 4

很清楚的就理解了LRU的机制,当get的key在cache中已经存在时,就将存储的内容放到最前面,get的key不存在时就返回-1。

put的新的key-value时,如果达到了容量上限,就删除一个最近最少使用的,实际上也是队尾的元素,然后将新的key-value存储到最前面。

因为我们每次要O(1)的复杂度,所以可以使用hashmap来get数据,而当容量达到上限时,要删除最近最少使用的,且要在最前面put进新的数据,要使用一个双向链表,来保证O(1)的时间复杂度。

java中可以使用LinkeHashMap来实现LRU缓存。

程序:

C++

class LRUCache {
public:
LRUCache(int capacity) {
cap = capacity;
} int get(int key) {
auto it = map.find(key);
if(it == map.end())
return -1;
l.splice(l.begin(), l, it->second);
return it->second->second;
} void put(int key, int value) {
auto it = map.find(key);
if(it != map.end()){
l.erase(it->second);
}
l.push_front(make_pair(key, value));
map[key] = l.begin();
if (map.size() > cap) {
int k = l.rbegin()->first;
l.pop_back();
map.erase(k);
}
}
private:
int cap;
list<pair<int, int>> l;
unordered_map<int, list<pair<int, int>>::iterator> map;
};

Java

class LRUCache {

    public LRUCache(int capacity) {
this.cap = capacity;
this.map = new HashMap<>();
this.dummyHead = new Node(0, 0);
this.tail = new Node(0, 0); dummyHead.prev = null;
dummyHead.next = tail;
tail.prev = dummyHead;
tail.next = null;
} public int get(int key) {
if(!map.containsKey(key))
return -1;
Node node = map.get(key);
removeNode(node);
addToHead(node);
return node.val;
} public void put(int key, int value) {
if(map.containsKey(key)){
Node node = map.get(key);
removeNode(node);
map.remove(key);
size--;
}
Node node = new Node(key, value);
map.put(key, node);
if(size < cap){
addToHead(node);
size++;
}else {
map.remove(tail.prev.key);
removeNode(tail.prev);
addToHead(node);
}
} private void removeNode(Node node){
node.prev.next = node.next;
node.next.prev = node.prev;
} private void addToHead(Node node){
node.next = dummyHead.next;
node.next.prev = node;
dummyHead.next = node;
node.prev = dummyHead;
} class Node{
int key;
int val;
Node prev;
Node next;
public Node(int key, int val){
this.key = key;
this.val = val;
}
}
private int cap;
private Map<Integer, Node> map;
private Node dummyHead;
private Node tail;
private int size;
}
class LRUCache {

    public LRUCache(int capacity) {
this.capacity = capacity;
map = new LinkedHashMap<>();
} public int get(int key) {
if(map.containsKey(key)) {
int value = map.get(key);
map.remove(key);
map.put(key, value);
return value;
}
return -1;
} public void put(int key, int value) {
if(map.containsKey(key)) {
map.remove(key);
}
map.put(key, value);
if(map.size() > capacity) {
map.remove(map.keySet().iterator().next());
}
}
private int capacity;
private LinkedHashMap<Integer, Integer> map;
}

LeetCode 146. LRU CacheLRU缓存机制 (C++/Java)的更多相关文章

  1. [leetcode]146. LRU CacheLRU缓存

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

  2. 重学数据结构(三)——使用单链表实现LRU淘汰缓存机制

    使用单链表实现LRU(Least Recently Used)淘汰缓存机制 需求:存在一个单链表,在单链表尾部的都是越早之前添加的元素. 当元素被访问到时,会添加进缓存(也就是这个单链表中). 如果这 ...

  3. Java实现 LeetCode 146 LRU缓存机制

    146. LRU缓存机制 运用你所掌握的数据结构,设计和实现一个 LRU (最近最少使用) 缓存机制.它应该支持以下操作: 获取数据 get 和 写入数据 put . 获取数据 get(key) - ...

  4. [Leetcode]146.LRU缓存机制

    Leetcode难题,题目为: 运用你所掌握的数据结构,设计和实现一个  LRU (最近最少使用) 缓存机制.它应该支持以下操作: 获取数据 get 和 写入数据 put . 获取数据 get(key ...

  5. LeetCode 146. LRU缓存机制(LRU Cache)

    题目描述 运用你所掌握的数据结构,设计和实现一个  LRU (最近最少使用) 缓存机制.它应该支持以下操作: 获取数据 get 和 写入数据 put . 获取数据 get(key) - 如果密钥 (k ...

  6. Leetcode 146. LRU 缓存机制

    前言 缓存是一种提高数据读取性能的技术,在计算机中cpu和主内存之间读取数据存在差异,CPU和主内存之间有CPU缓存,而且在内存和硬盘有内存缓存.当主存容量远大于CPU缓存,或磁盘容量远大于主存时,哪 ...

  7. 如何利用缓存机制实现JAVA类反射性能提升30倍

    一次性能提高30倍的JAVA类反射性能优化实践 文章来源:宜信技术学院 & 宜信支付结算团队技术分享第4期-支付结算部支付研发团队高级工程师陶红<JAVA类反射技术&优化> ...

  8. leetcode 146. LRU Cache 、460. LFU Cache

    LRU算法是首先淘汰最长时间未被使用的页面,而LFU是先淘汰一定时间内被访问次数最少的页面,如果存在使用频度相同的多个项目,则移除最近最少使用(Least Recently Used)的项目. LFU ...

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

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

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

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

随机推荐

  1. 红日安全vulnstack (一)

    网络拓扑图 靶机参考文章 CS/MSF派发shell 环境搭建 IP搭建教程 本机双网卡 65网段和83网段是自己本机电脑(虚拟机)中的网卡, 靶机外网的IP需要借助我们这两个网段之一出网 Kali ...

  2. 合阔智云核心生产系统切换到服务网格 ASM 的落地实践

    简介: 合阔智云提供了从全渠道交易管理到订单履约再到门店供应链完整的餐饮零售连锁解决方案,整个方案采取微服务设计,并深度使用了 Kubernetes 作为生产调度平台. 作者:刘如鸿   背景 合阔智 ...

  3. SKG 渠道中台借助 SAE + 大禹打造云原生 DevOPS,提效 60%

    简介: 新零售标杆 SKG 全面拥抱 Serverless,敏捷交付! 作者:陈列昂(SKG).昕辰.龙琛.黛忻 项目背景 未来穿戴健康科技股份有限公司(SKG)是一家专注为个人与家庭提供智能可穿戴健 ...

  4. 一个 Blink 小白的成长之路

    写在前面 写过blink sql的同学应该都有体会,明明写的时候就很顺滑,小手一抖,洋洋洒洒三百行代码,一气呵成.结果跑的时候,吞吐量就是上不去.导致数据延迟高,消息严重积压,被业务方疯狂吐槽.这时候 ...

  5. SQL 开发任务超 50% !滴滴实时计算的演进与优化

    摘要:Apache Flink 是一个分布式大数据处理引擎,可对有限数据流和无限数据流进行有状态计算.可部署在各种集群环境,对各种大小的数据规模进行快速计算.滴滴基于 Apache Flink 做了大 ...

  6. 容器环境自建数据库、中间件一键接入阿里云 Prometheus 监控

    ------------恢复内容开始------------ 阿里云Prometheus服务4月9日发布重大升级,支持容器环境下一键接入MySQL.Redis.MangoDB.ElasticSearc ...

  7. Hologres+Hologres+Flink流批一体首次落地4982亿背后的营销分析大屏Flink流批一体首次落地4982亿背后的营销分析大屏

    简介: 本篇将重点介绍Hologres在阿里巴巴淘宝营销活动分析场景的最佳实践,揭秘Flink+Hologres流批一体首次落地阿里双11营销分析大屏背后的技术考验. 概要:刚刚结束的2020天猫双1 ...

  8. 技术干货 | 闲鱼:一个优秀的 Push 平台,需要经历怎样的前世今生

    ​简介: mPaaS 消息推送服务,快速集成多家厂商 Push 通道,有效提高用户留存率,提升用户体验. 编者荐语: 点击这里,了解 mPaaS 消息推送服务,快速集成多家厂商 Push 通道,有效提 ...

  9. 2019-8-31-C#-简单读取文件

    title author date CreateTime categories C# 简单读取文件 lindexi 2019-08-31 16:55:58 +0800 2018-07-19 16:48 ...

  10. [Java]线程生命周期与线程通信

    [版权声明]未经博主同意,谢绝转载!(请尊重原创,博主保留追究权) https://www.cnblogs.com/cnb-yuchen/p/18162522 出自[进步*于辰的博客] 线程生命周期与 ...