LRU缓存算法的实现

题目描述
这个问题在leetcode146题中有具体介绍,具体为:
运用你所掌握的数据结构,设计和实现一个 LRU (最近最少使用) 缓存机制。它应该支持以下操作: 获取数据 get 和 写入数据 put 。
获取数据 get(key) - 如果密钥 (key) 存在于缓存中,则获取密钥的值(总是正数),否则返回 -1。
写入数据 put(key, value) - 如果密钥不存在,则写入其数据值。当缓存容量达到上限时,它应该在写入新数据之前删除最近最少使用的数据值,从而为新的数据值留出空间。
进阶:
你是否可以在 O(1) 时间复杂度内完成这两种操作?

思路
要在O(1)时间复杂度内完成get和put操作,key与value必须存入到map中,如何确定最近使用最少这一性质呢,这里涉及到最近使用时间的排序问题,每次改动必然导致相关数据的删除优先级顺序发生改变,因此可以考虑使用链表结构,tail存储最近一次访问的value。
具体Java语言实现为:采用HashMap存储key与value,双向链表结构表示数据value的访问顺序,为什么必须要用双向链表呢,因为我们需要删除操作,删除一个节点不光要得到该节点本身的指针,也需要操作其前驱节点的指针,而双向链表才能支持直接查找前驱,保证操作的时间复杂度 O(1)。
//参考左程云算法视频,对于当前访问的数据放在链表头部,当数据容量超过设定容量时,从头部删除。

import java.util.HashMap;
public class Code_02_LRU {

public static class Node {
	public V value;
	public Node last;
	public Node next;

	public Node(V value) {
		this.value = value;
	}
}

public static class NodeDoubleLinkedList {
	private Node head; //队头
	private Node tail; //队尾

	public NodeDoubleLinkedList() {
		this.head = null;
		this.tail = null;
	}

	public void addNode(Node newNode) {
		if (newNode == null) {
			return;
		}
		if (this.head == null) {
			this.head = newNode;
			this.tail = newNode;
		} else {
			this.tail.next = newNode;
			newNode.last = this.tail;
			this.tail = newNode;
		}
	}

	public void moveNodeToTail(Node node) {
		if (this.tail == node) {
			return;
		}
		if (this.head == node) {
			this.head = node.next;
			this.head.last = null;
		} else {
			node.last.next = node.next;
			node.next.last = node.last;
		}
		node.last = this.tail;
		node.next = null;
		this.tail.next = node;
		this.tail = node;
	}

	public Node removeHead() {
		if (this.head == null) {
			return null;
		}
		Node res = this.head;
		if (this.head == this.tail) {
			this.head = null;
			this.tail = null;
		} else {
			this.head = res.next;
			res.next = null;
			this.head.last = null;
		}
		return res;
	}

}

public static class MyCache {
	private HashMap> keyNodeMap;
	private HashMap, K> nodeKeyMap;
	private NodeDoubleLinkedList nodeList;
	private int capacity;

	public MyCache(int capacity) {
		if (capacity < 1) {
			throw new RuntimeException("should be more than 0.");
		}
		this.keyNodeMap = new HashMap>();
		this.nodeKeyMap = new HashMap, K>();
		this.nodeList = new NodeDoubleLinkedList();
		this.capacity = capacity;
	}

	public V get(K key) {
		if (this.keyNodeMap.containsKey(key)) {
			Node res = this.keyNodeMap.get(key);
			this.nodeList.moveNodeToTail(res);
			return res.value;
		}
		return null;
	}

	public void set(K key, V value) {
		if (this.keyNodeMap.containsKey(key)) {
			Node node = this.keyNodeMap.get(key);
			node.value = value;
			this.nodeList.moveNodeToTail(node);
		} else {
			Node newNode = new Node(value);
			this.keyNodeMap.put(key, newNode);
			this.nodeKeyMap.put(newNode, key);
			this.nodeList.addNode(newNode);
			if (this.keyNodeMap.size() == this.capacity + 1) {
				this.removeMostUnusedCache();
			}
		}
	}

	private void removeMostUnusedCache() {
		Node removeNode = this.nodeList.removeHead();
		K removeKey = this.nodeKeyMap.get(removeNode);
		this.nodeKeyMap.remove(removeNode);
		this.keyNodeMap.remove(removeKey);
	}

}

public static void main(String[] args) {
	MyCache testCache = new MyCache(3);
	testCache.set("A", 1);
	testCache.set("B", 2);
	testCache.set("C", 3);
	System.out.println(testCache.get("B"));
	System.out.println(testCache.get("A"));
	testCache.set("D", 4);
	System.out.println(testCache.get("D"));
	System.out.println(testCache.get("C"));
}

}

你可能感兴趣的:(算法,IT)