From 625832361cc8239f679c6f5744689dead901bd67 Mon Sep 17 00:00:00 2001 From: kyriewhluo Date: Thu, 15 Aug 2024 15:35:23 +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缓存策略的要求。 --- .../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; + } + } + +}