LeetCode 146. LRU CacheLRU缓存机制 (C++/Java)
题目:
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)的更多相关文章
- [leetcode]146. LRU CacheLRU缓存
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...
- 重学数据结构(三)——使用单链表实现LRU淘汰缓存机制
使用单链表实现LRU(Least Recently Used)淘汰缓存机制 需求:存在一个单链表,在单链表尾部的都是越早之前添加的元素. 当元素被访问到时,会添加进缓存(也就是这个单链表中). 如果这 ...
- Java实现 LeetCode 146 LRU缓存机制
146. LRU缓存机制 运用你所掌握的数据结构,设计和实现一个 LRU (最近最少使用) 缓存机制.它应该支持以下操作: 获取数据 get 和 写入数据 put . 获取数据 get(key) - ...
- [Leetcode]146.LRU缓存机制
Leetcode难题,题目为: 运用你所掌握的数据结构,设计和实现一个 LRU (最近最少使用) 缓存机制.它应该支持以下操作: 获取数据 get 和 写入数据 put . 获取数据 get(key ...
- LeetCode 146. LRU缓存机制(LRU Cache)
题目描述 运用你所掌握的数据结构,设计和实现一个 LRU (最近最少使用) 缓存机制.它应该支持以下操作: 获取数据 get 和 写入数据 put . 获取数据 get(key) - 如果密钥 (k ...
- Leetcode 146. LRU 缓存机制
前言 缓存是一种提高数据读取性能的技术,在计算机中cpu和主内存之间读取数据存在差异,CPU和主内存之间有CPU缓存,而且在内存和硬盘有内存缓存.当主存容量远大于CPU缓存,或磁盘容量远大于主存时,哪 ...
- 如何利用缓存机制实现JAVA类反射性能提升30倍
一次性能提高30倍的JAVA类反射性能优化实践 文章来源:宜信技术学院 & 宜信支付结算团队技术分享第4期-支付结算部支付研发团队高级工程师陶红<JAVA类反射技术&优化> ...
- leetcode 146. LRU Cache 、460. LFU Cache
LRU算法是首先淘汰最长时间未被使用的页面,而LFU是先淘汰一定时间内被访问次数最少的页面,如果存在使用频度相同的多个项目,则移除最近最少使用(Least Recently Used)的项目. LFU ...
- [LeetCode] 146. LRU Cache 最近最少使用页面置换缓存器
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...
- [LeetCode] 146. LRU Cache 近期最少使用缓存
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...
随机推荐
- 力扣350(java&python)-两个数组的交集 II(简单)
题目: 给你两个整数数组 nums1 和 nums2 ,请你以数组形式返回两数组的交集.返回结果中每个元素出现的次数,应与元素在两个数组中都出现的次数一致(如果出现次数不一致,则考虑取较小值).可以不 ...
- 力扣7(java)-整数反转(中等)
题目: 给你一个 32 位的有符号整数 x ,返回将 x 中的数字部分反转后的结果. 如果反转后整数超过 32 位的有符号整数的范围 [−231, 231 − 1] ,就返回 0. 假设环境不允许存 ...
- HarmonyOS NEXT应用开发—翻页动效案例
介绍 翻页动效是应用开发中常见的动效场景,常见的有书籍翻页,日历翻页等.本例将介绍如何通过ArkUI提供的显示动画接口animateTo实现翻页的效果. 效果图预览 使用说明 本例通过setInter ...
- LlamaIndex 高层次概念
本篇内容为您快速介绍在构建基于大型语言模型(LLM)的应用程序时会频繁遇到的一些核心概念. 增强检索生成(RAG) LLM 是基于海量数据训练而成,但并未涵盖您的具体数据.增强检索生成(Retriev ...
- [FAQ] MEMORY ALLOC FAILED: mmap with HUGETLB failed, attempting without it (you should fix your kernel)
cat /proc/meminfo | grep Huge sysctl vm.nr_hugepages=128 cat /proc/meminfo | grep Huge Link:https:// ...
- [Py] Failed to import pydot. You must install pydot and graphviz for `pydotprint` to work
当通过常规命令安装 pip install pydot 和 brew install graphviz 之后,在代码中 import pydot 依旧不生效. 比如:在 tensorflow 使用 t ...
- 使用 Kafka Assistant,为您的开发加速
简要介绍 快速查看所有 Kafka 集群,包括Brokers.Topics和Consumers 支持各种认证模式:PLAINTEXT.SASL_PLAINTEXT.SSL.SASL_SSL 对Kafk ...
- 003_Orcad菜单讲解与偏好设置
003_Orcad菜单讲解与偏好设置 菜单栏用的比较多的是File和Options项. 网格建议用lines,比较方便对齐. Auto Reference和Intertool Commuication ...
- Ubuntu(Linux)虚拟机与主机共享文件夹hgfs文件普通用户不能访问问题
参考文章: https://blog.csdn.net/weixin_42224577/article/details/108107554 在虚拟机中设置好添加共享文件后,普通用户并不能访问mnt文件 ...
- WEB服务与NGINX(17)- https协议及使用nginx实现https功能
目录 1. https协议及使用nginx实现https功能 1.1 https协议概述 1.2 TLS/SSL协议原理 1.3 https的实现原理 1.4 使用openssl申请证书 1.5 ng ...