From 625832361cc8239f679c6f5744689dead901bd67 Mon Sep 17 00:00:00 2001 From: kyriewhluo Date: Thu, 15 Aug 2024 15:35:23 +0800 Subject: [PATCH 1/2] =?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缓存策略的要求。 --- .../cn/whaifree/leetCode/Hash/LeeCode146.java | 126 ++++++++++++++++++ 1 file changed, 126 insertions(+) create mode 100644 src/main/java/cn/whaifree/leetCode/Hash/LeeCode146.java diff --git a/src/main/java/cn/whaifree/leetCode/Hash/LeeCode146.java b/src/main/java/cn/whaifree/leetCode/Hash/LeeCode146.java new file mode 100644 index 0000000..8f86c21 --- /dev/null +++ b/src/main/java/cn/whaifree/leetCode/Hash/LeeCode146.java @@ -0,0 +1,126 @@ +package cn.whaifree.leetCode.Hash; + +import cn.whaifree.leetCode.model.ListNode; +import org.junit.Test; + +import java.security.Key; +import java.util.HashMap; + +public class LeeCode146 { + + @Test + public void test() + { + LRUCache lruCache = new LRUCache(2); + lruCache.put(1, 1); + lruCache.put(2, 2); + System.out.println(lruCache.get(1)); + lruCache.put(3, 3); + System.out.println(lruCache.get(2)); + lruCache.put(4, 4); + System.out.println(lruCache.get(1)); + System.out.println(lruCache.get(3)); + System.out.println(lruCache.get(4)); + + + } + + class LRUCache { + + static class EntryNode { + Integer key = null; + Integer object = null; + EntryNode pre = null; + EntryNode after = null; + + public EntryNode(Integer object, EntryNode pre, EntryNode after) { + this.object = object; + this.pre = pre; + this.after = after; + } + + public EntryNode(Integer key ,Integer object) { + this.key = key; + this.object = object; + } + + public EntryNode() { + } + } + + HashMap caches; + EntryNode head; + EntryNode tail; + Integer size = null; + + + public LRUCache(int capacity) { + caches = new HashMap<>(); + size = capacity; + head = new EntryNode(); + tail = new EntryNode(); + head.after = tail; + tail.pre = head; + } + + /** + * 1. 先判断hashmap中有没有 + * 2. 如果有 + * + * @param key + * @return + */ + public int get(int key) { + EntryNode entryNode = caches.get(key); + if (entryNode == null) { + return -1; + } + deleteNode(entryNode); + addNode(entryNode); + return entryNode.object; + } + + public void put(int key, int value) { + + if (caches.containsKey(key)) { + EntryNode entryNode = caches.get(key); + entryNode.object = value; + deleteNode(entryNode); + addNode(entryNode); + return; + } + + EntryNode v = new EntryNode(key,value); + caches.put(key, v); + addNode(v); + if (caches.size() > size) { + //删除最早那个 + caches.remove(head.after.key); + deleteNode(head.after); + } + } + + + /** + * 删除某个节点 + * @param entryNode + */ + public void deleteNode(EntryNode entryNode) { + entryNode.pre.after = entryNode.after; + entryNode.after.pre = entryNode.pre; + } + + /** + * 在尾部增加某个节点 + * @param node + */ + public void addNode(EntryNode node) { + EntryNode pre = tail.pre; + pre.after = node; + node.pre = pre; + node.after = tail; + tail.pre = node; + } + } + +} From 32a8ad89b94e25a59e7fc087bb56f8f78b4a508b Mon Sep 17 00:00:00 2001 From: kyriewhluo Date: Thu, 15 Aug 2024 15:36:08 +0800 Subject: [PATCH 2/2] =?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) {