首页 技术 正文
技术 2022年11月23日
0 收藏 552 点赞 4,249 浏览 3049 个字

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

获取数据 get(key) – 如果密钥 (key) 存在于缓存中,则获取密钥的值(总是正数),否则返回 -1。
写入数据 put(key, value) – 如果密钥已经存在,则变更其数据值;如果密钥不存在,则插入该组「密钥/数据值」。当缓存容量达到上限时,它应该在写入新数据之前删除最久未使用的数据值,从而为新的数据值留出空间。进阶:
你是否可以在 O(1) 时间复杂度内完成这两种操作?

示例:

LRUCache cache = new LRUCache( 2 /* 缓存容量 */ );

cache.put(1, 1);
cache.put(2, 2);
cache.get(1); // 返回 1
cache.put(3, 3); // 该操作会使得密钥 2 作废
cache.get(2); // 返回 -1 (未找到)
cache.put(4, 4); // 该操作会使得密钥 1 作废
cache.get(1); // 返回 -1 (未找到)
cache.get(3); // 返回 3
cache.get(4); // 返回 4

代码:

package test3;import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;class Node {
int key;
int val;
Node next;
Node prev; public Node(int key, int val){
this.key = key;
this.val = val;
next = null;
prev = null;
}
}public class LruCache {
private int capacity;
private HashMap<Integer, Node> cacheMap;
private Node head, tail; public LruCache(int capacity) {
this.capacity = capacity;
this.cacheMap = new LinkedHashMap<>();
this.head = new Node(-1, -1);
this.tail = new Node(-1, -1);
head.next = tail;
head.prev = tail;
tail.next = head;
tail.prev = head;
} public int get(int key) {
if(!cacheMap.containsKey(key)) {
return -1;
}
Node node = cacheMap.get(key);
moveToHead(node);
return node.val;
} public void put(int key, int value) {
if (cacheMap.containsKey(key)) {
cacheMap.get(key).val = value;
moveToHead(cacheMap.get(key));
} else {
Node node = new Node(key, value);
if (cacheMap.size() >= this.capacity) {
Node rm = tail.prev;
deleteNode(rm);
cacheMap.remove(rm.key);
}
insertHead(node);
cacheMap.put(key, node);
}
} private void moveToHead(Node node) {
deleteNode(node);
insertHead(node);
} private void insertHead(Node node) {
Node next = head.next;
head.next = node;
node.prev = head;
node.next = next;
next.prev = node;
} private void deleteNode(Node node) {
Node front = node.prev;
Node end = node.next;
front.next = end;
end.prev = front;
} public void printCache() {
StringBuilder sb=new StringBuilder();
sb.append("["); List<String> ls=new ArrayList<String>();
for(int key:cacheMap.keySet()) {
Node value=cacheMap.get(key);
ls.add("("+key+","+value.val+")");
}
sb.append(String.join(",", ls)); sb.append("]");
System.out.println(sb.toString());
} public static void main(String[] args) throws Exception{
LruCache cache=new LruCache(2);
cache.put(1,1);
System.out.print(".....");
cache.printCache();
System.out.println(); cache.put(2,2);
System.out.print(".....");
cache.printCache();
System.out.println(); System.out.print(cache.get(1));
System.out.print(".....");
cache.printCache();
System.out.println(); cache.put(3, 3);
System.out.print(".....");
cache.printCache();
System.out.println(); System.out.print(cache.get(2));
System.out.print(".....");
cache.printCache();
System.out.println(); cache.put(4, 4);
System.out.print(".....");
cache.printCache();
System.out.println(); System.out.print(cache.get(1));
System.out.print(".....");
cache.printCache();
System.out.println(); System.out.print(cache.get(3));
System.out.print(".....");
cache.printCache();
System.out.println(); System.out.print(cache.get(4));
System.out.print(".....");
cache.printCache();
System.out.println();
}
}

输出:

.....[(1,1)].....[(1,1),(2,2)]1.....[(1,1),(2,2)].....[(1,1),(3,3)]-1.....[(1,1),(3,3)].....[(3,3),(4,4)]-1.....[(3,3),(4,4)]3.....[(3,3),(4,4)]4.....[(3,3),(4,4)]

–2020年5月11日–

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,999
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,511
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,357
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,140
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,770
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,848