146. LRU Cache - Hard

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.

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

Example:

LRUCache cache = new LRUCache( 2 /* capacity */ );

cache.put(1, 1);
cache.put(2, 2);
cache.get(1);       // returns 1
cache.put(3, 3);    // evicts key 2
cache.get(2);       // returns -1 (not found)
cache.put(4, 4);    // evicts key 1
cache.get(1);       // returns -1 (not found)
cache.get(3);       // returns 3
cache.get(4);       // returns 4

HashMap + Double LinkedList

首先定义一个Node类,用来构建double linked list,包括key, val, next, prev。两个参数count, capacity,count用来给cache里的元素计数。LRUcache初始化的时候,还要定义两个dummy Node,head和tail,方便取和放链表首(head.next)、尾(tail.prev)的元素。

get:如果map中存在该key,从map中取出node,保存这个node对应的value,在链表中删掉这个节点,再把这个节点插入到链表头部。如果map中不存在该key,返回-1。

put:根据map中有无该key,分两种情况

1. map中无key。根据新的(key, value)新建一个node,把node放进map里。如果没有超过容量,把当前node放到double linked list的头部,并且count增加1;如果超过容量,删除链表尾部的节点(注意除了链表里,map里也要删除!),并把当前node插入到头部。

2. map中有key。从map中取出node,并赋值为当前value,在链表中删掉这个节点,再把这个节点插入到链表头部。

删除节点 和 把节点插入到链表头部 可以用两个辅助函数来表示。

时间:O(1),空间:O(N)

class LRUCache {
    class Node {
        int key, val;
        Node next, prev;
        public Node(int key, int val) {
            this.key = key;
            this.val = val;
        }
    }
    HashMap<Integer, Node> map;
    Node head, tail;
    int count, capacity;

    public LRUCache(int capacity) {
        this.capacity = capacity;
        count = 0;
        map = new HashMap<>();
        head = new Node(0, 0);
        tail = new Node(0, 0);
        head.next = tail;
        head.prev = null;
        tail.next = null;
        tail.prev = head;
    }

    private void deleteNode(Node node) {
        node.prev.next = node.next;
        node.next.prev = node.prev;
    }

    private void addToHead(Node node) {
        head.next.prev = node;
        node.next = head.next;
        head.next = node;
        node.prev = head;
    }

    public int get(int key) {
        if(map.containsKey(key)) {
            Node node = map.get(key);
            int val = node.val;
            deleteNode(node);
            addToHead(node);
            return val;
        }
        return -1;
    }

    public void put(int key, int value) {
        if(map.containsKey(key)) {
            Node node = map.get(key);
            node.val = value;
            deleteNode(node);
            addToHead(node);
        } else {
            Node node = new Node(key, value);
            map.put(key, node);
            if(count < capacity) {
                addToHead(node);
                count++;
            } else {
                map.remove(tail.prev.key);
                deleteNode(tail.prev);
                addToHead(node);
            }
        }
    }
}

/**
 * Your LRUCache object will be instantiated and called as such:
 * LRUCache obj = new LRUCache(capacity);
 * int param_1 = obj.get(key);
 * obj.put(key,value);
 */

原文地址:https://www.cnblogs.com/fatttcat/p/10048111.html

时间: 2024-10-28 08:52:50

146. LRU Cache - Hard的相关文章

No.146 LRU Cache

No.146 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, otherwi

146. LRU Cache(js)

146. LRU Cache 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

146. LRU Cache

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

Java for LeetCode 146 LRU Cache 【HARD】

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

[leetcode]146. 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(

[email&#160;protected] [146] LRU Cache (TreeMap)

https://leetcode.com/problems/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 exis

8.12 [LeetCode] 146 LRU Cache

Question link 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

[leedcode 146] 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(

【hard】146. LRU Cache

其实也米有很难……只是c++11的api这么好用的吗 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 cach