[Leetcode-146] LRU Cache 最近最少使用页面置换算法
来源:互联网 发布:阿沁淘宝店 编辑:程序博客网 时间:2024/06/06 00:56
- 题目概要
- AC 代码
0. 题目概要
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.
Follow up:
Could you do both operations in O(1) time complexity?
Example:
//新建一个缓存容量为2的LRUCache对象 cacheLRUCache cache = new LRUCache( 2 /* capacity */ ); cache.put(1, 1);cache.put(2, 2);cache.get(1); // returns 1 1被使用一次cache.put(3, 3); // evicts key 2 此时2为最近最少使用的, 所以驱逐淘汰2 , 替换为3cache.get(2); // returns -1 (not found) 因为2被淘汰,返回-1cache.put(4, 4); // evicts key 1 淘汰1 用4替换1cache.get(1); // returns -1 (not found) 因为1被淘汰,返回-1cache.get(3); // returns 3 读取3cache.get(4); // returns 4 读取4
AC 代码
/** * Your LRUCache object will be instantiated and called as such: * LRUCache obj = new LRUCache(capacity); * int param_1 = obj.get(key); * obj.put(key,value); */class LRUCache { public LRUCache(int capacity) { } public int get(int key) { } public void put(int key, int value) { }}
/** * Your LRUCache object will be instantiated and called as such: * LRUCache obj = new LRUCache(capacity); * int param_1 = obj.get(key); * obj.put(key,value); */import java.util.ArrayList;import java.util.HashMap;public class LRU { public int cap; public ArrayList<Integer> list = new ArrayList<Integer>(); public HashMap<Integer,Integer> map = new HashMap<Integer, Integer>(); public LRU(int capacity){ this.cap = capacity; } public int get(int key){ if(map.containsKey(key)){ int index = list.indexOf(key);//确定key 在list的 index 下标 list.remove(index); // 原来链表中删除 list.add(0, key); //添加到链表中的对前面 【最近访问】 return map.get(key);//返回key 对应的value } return -1;//如若key不存在 返回-1 } public void put(int key, int value){ if(map.containsKey(key)){ int index = list.indexOf(key); list.remove(index); list.add(0, key); // 移动到最近访问的位置 map.put(key, value); }else{ list.add(0, key); map.put(key, value); if(list.size() > cap){ int rm = list.get(list.size()-1); //获得链表最后一个超出缓存大小的元素的 下标rm list.remove(list.size()-1); //从链表中删除末尾元素 超出缓存大小的元素 map.remove(rm); // 从哈希表中 删除对应的 溢出队列的缓存元素 } } }}
下面贴出, 其他人提交的最快的代码:
import java.util.LinkedHashMap;import java.util.Map;public class LRUCache { private LinkedHashMap<Integer, Integer> cache; public LRUCache(int capacity) { cache = new LinkedHashMap<Integer, Integer>(capacity, 1f, true) { protected boolean removeEldestEntry(Map.Entry eldest) { return size() > capacity; } }; } public int get(int key) { return cache.getOrDefault(key, -1); } public void put(int key, int value) { cache.put(key, value); }}
阅读全文
0 0
- [Leetcode-146] LRU Cache 最近最少使用页面置换算法
- LRU(最近最少使用页面置换算法)淘汰算法
- LRU(最近最少使用页面置换算法)淘汰算法
- 操作系统页面置换算法之最近最少使用算法(LRU)
- LRU最近最久未使用页面置换算法
- LRU(Least Recently Used)最近最少使用页面置换算法
- 【python学习笔记】3:LRU(最近最少使用页面置换)算法
- leetcode LRU Cache(高级缓存的最近最少使用算法实现)
- 页面置换算法-最近最久未用(LRU)
- 缓存淘汰算法--LRU算法【最近最少使用算法LRU置换策略适用于热点数据比较多的场景】
- 最近最久未使用(LRU)置换算法
- 最近最久未使用(LRU)置换算法
- LRU算法,最近最少使用算法
- LRU算法,最近最少使用算法
- 自适应Lru(最近最少使用)算法
- 最近最少使用(LRU)算法模拟--LeastRecentPage
- 最近最少使用缓存算法LRU
- 页面置换算法LRU
- PHP编程实现阳历转换为阴历的方法实例
- JavaScript进阶:JS的全局变量和全局函数
- 定时任务quartz
- Qt5开发学习之文本编辑功能(七)
- 相对路径、绝对路径、mysql及jdbc基础
- [Leetcode-146] LRU Cache 最近最少使用页面置换算法
- struct ip 和struct iphdr的差别 , <netinet/*.h>和<linux/*.h>
- POJ 2769 Reduced ID Numbers
- Accounting.js库的使用
- Docker 删除image
- 【Linux 学习】 文件系统
- 如何防止ISE综合时信号不被优化掉
- 年中总结
- Neo4j CQL -(4)- MATCH & RETURN匹配和返回