首页 > 代码库 > Leetcode: LRU Cache
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.
难度:90。这是一道非常综合的题目,主要应用在操作系统的资源管理中。
按照题目要求,要实现get和set功能,为了满足get功能:随机访问的需求(lookup)我们首先想到的一般是用数组,如果用链表会有O(n)的访问时间。然而他又有另一个条件就是要维护least used的队列,也就是说经常用的放在前面,用的少的放在后面。这样当资源超过cache的容积的时候就可以把用得最少的资源删掉。这就要求我们要对节点有好的删除和插入操作,这个要求又会让我们想到用链表,其删除插入是O(1),而数组的删除和插入是O(n)复杂度的。
lookup, insert operations in O(1) leads to use HashMap.
Hashtable也是一个不错的选择,本来它的insert, delete, lookup操作amortized代价都是O(1), 但是,这里delete无法O(1). 这是因为这里困难的是得找到合乎条件的节点去删除,需要花时间在search这个Least Recently Used节点。想过用一种方法,if we use a time stamp on each entry, deleting the one with oldest access time stamp, But it will take O(n) time cost. 所以,如果只用HashMap, 删除最老的节点需要O(n).
那么我们能不能维护一个数据结构使得访问操作和插入删除操作都是O(1)复杂度的呢?答案是肯定的。这个数据结构比较复杂,是用一个hash表加上一个双向链表来实现。
这里为什么使用Doubly linkd list 而不使用 Linked list的原因是:
当访问一个节点后,我们需要将他从原来的list中删除,然后将它插入到头节点处,删除过程中需要将其前后节点连接起来,单链表做不到。
查询:
- 根据键值查询hashmap,若命中,则返回节点,否则返回null。
- 从双向链表中删除命中的节点,将其重新插入到表头。
- 所有操作的复杂度均为O(1)。
插入:
- 将新的节点关联到Hashmap
- 如果Cache满了,删除双向链表的尾节点,同时删除Hashmap对应的记录
- 将新的节点插入到双向链表中头部
更新:
- 和查询相似
删除:
- 从双向链表和Hashmap中同时删除对应的记录。
通过hash表访问结点我们可以认为是O(1)的操作(假设hash函数足够好),然后双向链表的插入删除操作也是O(1)的操作。如此我们便实现了用O(1)时间来完成所有LRU cache的操作。空间上就是对于每一个资源有一个hash表的的项以及一个对应的结点(包含前后指针和资源的<key, value>)。代码如下:
1 public class LRUCache { 2 class Node { 3 Node prev, next; 4 int key; 5 int value; 6 public Node(int key, int value) { 7 this.key = key; 8 this.value =http://www.mamicode.com/ value; 9 }10 }11 12 int capacity;13 int num;14 Node first, last;15 HashMap<Integer, Node> map;16 17 public LRUCache(int capacity) {18 this.capacity = capacity;19 this.num = 0;20 this.first = null;21 this.last = null;22 this.map = new HashMap<Integer, Node>();23 }24 25 public int get(int key) {26 Node n = map.get(key);27 if (n == null) { //no find the key28 return -1;29 }30 int val = n.value;31 if (n != last) { //find the key, and the node is not the last, need to put the node to the last of the LinkedList32 if (n == first) { //delete the node from its original place, when the node is the first33 first.next.prev = null;34 first = first.next;35 }36 else { //delete the node from its original place, when the node is neither the first nor the last37 n.next.prev = n.prev;38 n.prev.next = n.next;39 }40 n.next = null; //put the node to the last of the Linkedlist41 n.prev = last;42 last.next = n;43 last = last.next;44 }45 return val; //return the value of the key node46 }47 48 public void set(int key, int value) {49 Node n = map.get(key);50 if (n != null) { //find the node, need to change its value and put to last51 if (n != last) { //delete from its original place and put to last52 if (n == first) {53 first.next.prev = null;54 first = first.next;55 }56 else {57 n.next.prev = n.prev;58 n.prev.next = n.next;59 }60 n.next = null;61 n.prev = last;62 last.next = n;63 last = last.next;64 }65 n.value = http://www.mamicode.com/value; //change the value66 }67 else { //not find the node, need to create one, put to the last68 Node newNode = new Node(key, value);69 70 if (num >= capacity) { //no capacity, need to delete first Node71 map.remove(first.key);72 first = first.next;73 if (first != null) first.prev = null;74 else last = null;75 num--;76 }77 if (num == 0) {78 first = newNode;79 last = newNode;80 }81 else {82 newNode.next = null;83 newNode.prev = last;84 last.next = newNode;85 last = last.next;86 }87 num++;88 map.put(key, newNode);89 }90 }91 }
Leetcode: LRU Cache