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

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

    The cache is initialized with a positive capacity.

    Follow up:
    Could you do both operations in O(1) time complexity?

    Example:

    1. LRUCache cache = new LRUCache( 2 /* capacity */ );
    2. cache.put(1, 1);
    3. cache.put(2, 2);
    4. cache.get(1); // returns 1
    5. cache.put(3, 3); // evicts key 2
    6. cache.get(2); // returns -1 (not found)
    7. cache.put(4, 4); // evicts key 1
    8. cache.get(1); // returns -1 (not found)
    9. cache.get(3); // returns 3
    10. cache.get(4); // returns 4

    题意

    实现一个LRU缓存机制:查询时,如果不存在目标key,则返回-1,否则返回key对应的value;插入时,如果缓存已满,则将访问时间最远的(key, value)对删去,存入新的(key, value)对。

    思路

    主要思想是维护一个类数组结构,越近访问的数据越靠近头部,越远访问的数据越靠近尾部。每次查询到旧值或访问新值时,将该值从原位置取出并插入到头部;需要删除数据时,只要删去尾部的数据。

    具体实现使用双向链表+HashMap:双向链表使得删除和移动结点的复杂度为146. LRU Cache (M) - 图1;HashMap用于创建索引,将key值映射到对应的结点,使结点查询复杂度也为146. LRU Cache (M) - 图2


    代码实现

    1. class LRUCache {
    2. // 双向链表结点
    3. class Node {
    4. int key, val;
    5. Node prev, next;
    6. public Node(int key, int val) {
    7. this.key = key;
    8. this.val = val;
    9. }
    10. }
    11. private Node head, tail;
    12. private int capacity;
    13. private Map<Integer, Node> hash;
    14. public LRUCache(int capacity) {
    15. this.capacity = capacity;
    16. hash = new HashMap<>();
    17. // dummy结点可以大大简化步骤
    18. head = new Node(-1, -1);
    19. tail = new Node(-1, -1);
    20. head.next = tail;
    21. }
    22. public int get(int key) {
    23. if (!hash.containsKey(key)) {
    24. return -1;
    25. }
    26. // 查询到存在,则将原结点拎出插入到头结点之后
    27. Node target = hash.get(key);
    28. removeFromPos(target);
    29. moveToFirst(target);
    30. return target.val;
    31. }
    32. public void put(int key, int value) {
    33. if (!hash.containsKey(key)) {
    34. // 需要插入新值,则新建结点并将其移动到头结点后
    35. Node temp = new Node(key, value);
    36. hash.put(key, temp);
    37. moveToFirst(temp);
    38. } else {
    39. // 更新旧值,并将原结点取出插入到头结点之后
    40. Node target = hash.get(key);
    41. target.val = value;
    42. removeFromPos(target);
    43. moveToFirst(target);
    44. }
    45. // 超出容量,则删去尾结点之前的结点
    46. if (hash.size() == capacity + 1) {
    47. hash.remove(tail.prev.key);
    48. removeFromPos(tail.prev);
    49. }
    50. }
    51. // 将目标结点移动到头结点之后
    52. private void moveToFirst(Node target) {
    53. target.next = head.next;
    54. head.next.prev = target;
    55. target.prev = head;
    56. head.next = target;
    57. }
    58. // 将目标结点从链中取出
    59. private void removeFromPos(Node target) {
    60. target.prev.next = target.next;
    61. target.next.prev = target.prev;
    62. target.next = null;
    63. target.prev = null;
    64. }
    65. }