← 返回 citadel 的题目列表LRU Cache Implementation Test
类型:online_judge
Implement an LRU (Least Recently Used) cache mechanism. Requirements:
Implement the LRUCache class:
LRUCache(int capacity) initializes the LRU cache with a positive integer capacity.
int get(int key) returns the value of the key if the key exists in the cache, otherwise returns -1.
void put(int key, int value) Updates the value of the key if it exists. Otherwise, adds the key-value pair to the cache. If the cache reaches its capacity, remove the least recently used item to make room for the new entry.
Provide at least five test cases to validate your implementation.
Assume a doubly-linked list DLL is pre-defined for storing the keys and values in the cache.
Example
Input
LRUCache 2
put 1 1
put 2 2
get 1
put 3 3
get 2
put 4 4
get 1
get 3
get 4