[LeetCode]LRU Cache

题目描述

Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set.

get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.
set(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.


解题思路


采用 双链表 + hashtable实现原理:
将Cache的所有位置都用双连表连接起来,当一个位置被命中之后,就将通过调整链表的指向,将该位置调整到链表头的位置,新加入的Cache直接加到链表头中。这样,在多次进行Cache操作后,最近被命中的,就会向链表头方向移动,而没有命中的,则向链表后面移动,链表尾则表示最近最少使用的Cache。当需要替换内容时候,链表的最后位置就是最少被命中的位置,我们只需要淘汰链表最后的部分即可。

代码

public class LRUCache {

		private int cacheSize;
		private Hashtable<Integer, Entry> nodes;// 缓存容器
		private int currentSize;
		private Entry first = null;// 链表头
		private Entry last = null;// 链表尾

		public LRUCache(int capacity) {
			currentSize = 0;
			cacheSize = capacity;
			nodes = new Hashtable<Integer, Entry>(capacity);// 缓存容器
		}

		/**
		 * 获取缓存中对象,并把它放在最前面
		 */
		public int get(int key) {
			Entry node = nodes.get(key);
			if (node != null) {
				moveToHead(node);
				return node.value;
			} else {
				return -1;
			}
		}

		/**
		 * 添加 entry到hashtable, 并把entry
		 */
		public void set(int key, int value) {
			// 先查看hashtable是否存在该entry, 如果存在,则只更新其value
			Entry node = nodes.get(key);
			if (node == null) {
				// 缓存容器是否已经超过大小.
				if (currentSize >= cacheSize) {
					nodes.remove(last.key);
					removeLast();
				} else {
					currentSize++;
				}
				node = new Entry(key, value);
			}
			node.value = value;
			// 将最新使用的节点放到链表头,表示最新使用的.
			moveToHead(node);
			nodes.put(key, node);
		}

		/**
		 * 将entry删除, 注意:删除操作只有在cache满了才会被执行
		 */
		public void remove(int key) {
			Entry node = nodes.get(key);
			// 在链表中删除
			if (node != null) {
				if (node.prev != null) {
					node.prev.next = node.next;
				}
				if (node.next != null) {
					node.next.prev = node.prev;
				}
				if (last == node)
					last = node.prev;
				if (first == node)
					first = node.next;
			}
			// 在hashtable中删除
			nodes.remove(key);
		}

		/**
		 * 删除链表尾部节点,即使用最后 使用的entry
		 */
		private void removeLast() {
			// 链表尾不为空,则将链表尾指向null. 删除连表尾(删除最少使用的缓存对象)
			if (last != null) {
				if (last.prev != null) {
					last.prev.next = null;
				} else {
					first = null;
				}
				last = last.prev;
			}
		}

		/**
		 * 移动到链表头,表示这个节点是最新使用过的
		 */
		private void moveToHead(Entry node) {
			if (node == first)
				return;
			if (node.prev != null)
				node.prev.next = node.next;
			if (node.next != null)
				node.next.prev = node.prev;
			if (last == node)
				last = node.prev;
			if (first != null) {
				node.next = first;
				first.prev = node;
			}
			first = node;
			node.prev = null;
			if (last == null) {
				last = first;
			}
		}

		/*
		 * 清空缓存
		 */
		public void clear() {
			first = null;
			last = null;
			currentSize = 0;
		}

		class Entry {
			Entry prev;// 前一节点
			Entry next;// 后一节点
			int value;// 值
			int key;// 键

			public Entry() {
				prev = null;
				next = null;
			}

			public Entry(int key, int value) {
				this.key = key;
				this.value = value;
			}
		}
	}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值