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.
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set.
get
set
get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.
get(key)
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.
set(key, value)
为最近最少使用(LRU)缓存策略设计一个数据结构,它应该支持以下操作:获取数据(get)和写入数据(set)。
获取数据get(key):如果缓存中存在key,则获取其数据值(通常是正数),否则返回-1。
写入数据set(key, value):如果key还没有在缓存中,则写入其数据值。当缓存达到上限,它应该在写入新数据之前删除最近最少使用的数据用来腾出空闲位置。
双向链表加哈希表
缓存讲究的就是快,所以我们必须做到O(1)的获取速度,这样看来只有哈希表可以胜任。但是哈希表无序的,我们没办法在缓存满时,将最早更新的元素给删去。那么是否有一种数据结构,可以将先进的元素先出呢?那就是队列。所以我们将元素存在队列中,并用一个哈希表记录下键值和元素的映射,就可以做到O(1)获取速度,和先进先出的效果。然而,当我们获取一个元素时,还需要把这个元素再次放到队列头,这个元素可能在队列的任意位置,可是队列并不支持对任意位置的增删操作。而最适合对任意位置增删操作的数据结构又是什么呢?是链表。我可以用链表来实现一个队列,这样就同时拥有链表和队列的特性了。不过,如果仅用单链表的话,在任意位置删除一个节点还是很麻烦的,要么记录下该节点的上一个节点,要么遍历一遍。所以双向链表是最好的选择。我们用双向链表实现一个队列用来记录每个元素的顺序,用一个哈希表来记录键和值的关系,就行了。
public class Solution { private int capacity; private HashMap<Integer, Node> map = new HashMap<>(); private Node head = new Node(-1, -1), tail = new Node(-1, -1); private class Node { Node prev, next; int val, key; public Node(int key, int val) { this.val = val; this.key = key; prev = null; next = null; } // @Override // public String toString() { // return "(" + key + ", " + val + ") " + "last:" // + (prev == null ? "null" : "node"); // } } public Solution(int capacity) { this.capacity = capacity; tail.prev = head; head.next = tail; } public int get(int key) { if (!map.containsKey(key)) { return -1; } // remove current Node currentNode = map.get(key); currentNode.prev.next = currentNode.next; currentNode.next.prev = currentNode.prev; // move current to tail; moveToTail(currentNode); return map.get(key).val; } public void set(int key, int value) { if (get(key) != -1) { map.get(key).val = value; return; } if (map.size() == capacity) { map.remove(head.next.key); head.next = head.next.next; head.next.prev = head; } Node insert = new Node(key, value); map.put(key, insert); moveToTail(insert); } private void moveToTail(Node current) { current.prev = tail.prev; tail.prev = current; current.prev.next = current; current.next = tail; } }
Copyright© 2013-2020
All Rights Reserved 京ICP备2023019179号-8