146. LRU 缓存机制

146. LRU 缓存机制

题目描述

运用你所掌握的数据结构,设计和实现一个 LRU (最近最少使用) 缓存机制

实现 LRUCache 类:

  • LRUCache(int capacity) 以正整数作为容量 capacity 初始化 LRU 缓存

  • int get(int key) 如果关键字 key 存在于缓存中,则返回关键字的值,否则返回 -1

  • void put(int key, int value) 如果关键字已经存在,则变更其数据值;如果关键字不存在,则插入该组「关键字-值」。当缓存容量达到上限时,它应该在写入新数据之前删除最久未使用的数据值,从而为新的数据值留出空间。

进阶:你是否可以在 O(1) 时间复杂度内完成这两种操作?

示例:

输入
["LRUCache", "put", "put", "get", "put", "get", "put", "get", "get", "get"]
[[2], [1, 1], [2, 2], [1], [3, 3], [2], [4, 4], [1], [3], [4]]
输出
[null, null, null, 1, null, -1, null, -1, 3, 4]

解释
LRUCache lRUCache = new LRUCache(2);
lRUCache.put(1, 1); // 缓存是 {1=1}
lRUCache.put(2, 2); // 缓存是 {1=1, 2=2}
lRUCache.get(1);    // 返回 1
lRUCache.put(3, 3); // 该操作会使得关键字 2 作废,缓存是 {1=1, 3=3}
lRUCache.get(2);    // 返回 -1 (未找到)
lRUCache.put(4, 4); // 该操作会使得关键字 1 作废,缓存是 {4=4, 3=3}
lRUCache.get(1);    // 返回 -1 (未找到)
lRUCache.get(3);    // 返回 3
lRUCache.get(4);    // 返回 4

提示:

  • 1 <= capacity <= 3000

  • 0 <= key <= 3000

  • 0 <= value <= 10^4

  • 最多调用 3 * 10^4getput


题解:

双链表 + 哈希。

假设双向链表从尾部到首部使用优先级依次递减,那么:

  • put(key, value) 则将节点插入尾部
  • get(key) ,若 key 存在,将其插入尾部
  • 删除时,将首部节点删除即可,并从哈希表中删除 key 的记录

时间复杂度: O ( 1 ) O(1) O(1)

额外空间复杂度: O ( c a p a c i t y ) O(capacity) O(capacity)

class LRUCache {
    
    
public:
    LRUCache(int capacity) : max_size(capacity) {
    
     }
    
    int get(int key) {
    
    
        auto iter = hash_map.find( key );
        if ( iter != hash_map.end() ) {
    
    
            lst.splice( lst.end(), lst, iter->second );
            return iter->second->second;
        } else return -1;
    }
    
    void put(int key, int value) {
    
    
        auto iter = hash_map.find( key );
        if ( iter != hash_map.end() ) {
    
    
            lst.splice( lst.end(), lst, iter->second );
            iter->second->second = value;
        } else {
    
    
            hash_map[key] = lst.insert( lst.end(), {
    
    key, value} );
            if ( lst.size() > max_size ) {
    
    
                hash_map.erase( lst.front().first );
                lst.pop_front();
            }
        }
    }
private:
    using key_value_pair = pair<int, int>;
    using list_iterator = list<key_value_pair>::iterator;

    size_t max_size;
    list<key_value_pair> lst;
    unordered_map<int, list_iterator> hash_map;
};
/*
时间:92ms,击败:95.82%
内存:39MB,击败:71.53%
*/

贴一个手写双链表的版本,添加一个虚拟头节点和尾节点,方便操作。

class DLinkedNode {
    
    
public:
    DLinkedNode( int _key = 0, int _val = 0 ) : key(_key), val(_val), next(nullptr), prev(nullptr) {
    
     }
private:
    int key, val;
    DLinkedNode *next, *prev;
    friend class LRUCache;
};

class LRUCache {
    
    
public:
    void _insert (int key, int value) {
    
    
        DLinkedNode *node = new DLinkedNode( key, value );

        node->next = tail;
        node->prev = tail->prev;
        tail->prev->next = node;
        tail->prev = node;

        hash[key] = node;
        ++size;
    }

    void _delete () {
    
    
        auto node = head->next;
        head->next = node->next;
        node->next->prev = head;
        hash.erase( node->key );
        delete node;
        --size;
    }

    void remove( DLinkedNode* node ) {
    
    
        node->prev->next = node->next;
        node->next->prev = node->prev;

        node->next = tail;
        node->prev = tail->prev;
        node->prev->next = node;
        tail->prev = node;
    }

    LRUCache(int capacity) : max_size(capacity), size(0) {
    
     
        head = new DLinkedNode();
        tail = new DLinkedNode();
        head->next = tail;
        tail->prev = head;
    }
    
    int get(int key) {
    
    
        auto iter = hash.find( key );
        if ( iter == hash.end() ) return -1;
        else {
    
    
            remove( iter->second );
            return iter->second->val;
        }
    }
    
    void put(int key, int value) {
    
    
        auto iter = hash.find( key );
        if ( iter == hash.end() ) {
    
    
            _insert( key, value );
            if ( size > max_size ) _delete();
        } else {
    
    
            iter->second->val = value;
            remove( iter->second );
        }
    }
private:
    DLinkedNode *head, *tail;
    int max_size, size;
    unordered_map<int, DLinkedNode*> hash;
};
/*
时间:88ms,击败:97.84%
内存:38.8MB,击败:94.56%
*/

猜你喜欢

转载自blog.csdn.net/MIC10086/article/details/114179476