leetcode/include/s0146_lru_cache.hpp

51 lines
1.2 KiB
C++
Raw Normal View History

2023-03-18 00:40:30 +00:00
#ifndef S0146_LRU_CACHE_HPP
#define S0146_LRU_CACHE_HPP
#include <cstdlib>
#include <unordered_map>
class LRUCache {
public:
/**
* @brief Least Recently Used Cache
*
* 使
*
* @param capacity
*/
LRUCache(int capacity);
/**
* @brief
*
* @param key
* @return
*/
int get(int key);
/**
* @brief
*
* @param key
* @param value
*/
void put(int key, int value);
private:
struct CacheNode {
int key;
int value;
CacheNode *next;
CacheNode *prev;
CacheNode(int key, int value)
: key(key), value(value), next(nullptr), prev(nullptr){};
CacheNode(int key, int value, CacheNode *next, CacheNode *prev)
: key(key), value(value), next(next), prev(prev){};
};
CacheNode *head;
CacheNode *tail;
int capacity;
std::unordered_map<int, CacheNode *> map; // 键值是 key值是该节点的指针
void moveToHead(CacheNode *node); // 将节点移动到头部
};
#endif