From 32a8ad89b94e25a59e7fc087bb56f8f78b4a508b Mon Sep 17 00:00:00 2001 From: kyriewhluo Date: Thu, 15 Aug 2024 15:36:08 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E7=8E=B0LRU=E7=BC=93=E5=AD=98?= =?UTF-8?q?=E6=9C=BA=E5=88=B6=E4=BB=A5=E8=A7=A3=E5=86=B3LeeCode146?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 实现了一个基于哈希表和双向链表的LRU(最近最少使用)缓存机制。该机制通过在双向链表的尾部添加最近使用的元素,并从头部移除最近最少使用的元素,以维护一个固定大小的缓存。当缓存满时,通过移除头部元素来腾出空间。此实现支持get和put操作,符合LRU缓存策略的要求。 --- .../java/cn/whaifree/leetCode/Hash/LeeCode146.java | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/main/java/cn/whaifree/leetCode/Hash/LeeCode146.java b/src/main/java/cn/whaifree/leetCode/Hash/LeeCode146.java index 8f86c21..6ce4d35 100644 --- a/src/main/java/cn/whaifree/leetCode/Hash/LeeCode146.java +++ b/src/main/java/cn/whaifree/leetCode/Hash/LeeCode146.java @@ -9,8 +9,7 @@ import java.util.HashMap; public class LeeCode146 { @Test - public void test() - { + public void test() { LRUCache lruCache = new LRUCache(2); lruCache.put(1, 1); lruCache.put(2, 2); @@ -39,7 +38,7 @@ public class LeeCode146 { this.after = after; } - public EntryNode(Integer key ,Integer object) { + public EntryNode(Integer key, Integer object) { this.key = key; this.object = object; } @@ -48,7 +47,7 @@ public class LeeCode146 { } } - HashMap caches; + HashMap caches; EntryNode head; EntryNode tail; Integer size = null; @@ -90,7 +89,7 @@ public class LeeCode146 { return; } - EntryNode v = new EntryNode(key,value); + EntryNode v = new EntryNode(key, value); caches.put(key, v); addNode(v); if (caches.size() > size) { @@ -103,6 +102,7 @@ public class LeeCode146 { /** * 删除某个节点 + * * @param entryNode */ public void deleteNode(EntryNode entryNode) { @@ -112,6 +112,7 @@ public class LeeCode146 { /** * 在尾部增加某个节点 + * * @param node */ public void addNode(EntryNode node) {