← 返回 linkedin 的题目列表Implement LRU Cache
类型:online_judge
Implement a LRU (Least Recently Used) cache class. The class should support get and set methods:
get(key): Returns the value to which the specified key is mapped, or -1 if this cache contains no mapping for the key.
set(key, value): Inserts a key-value pair into the cache if the key does not already exist. If the cache has reached its capacity, it should invalidate the least recently used item before inserting a new item.
The cache's capacity is set during object instantiation. Provide multiple test cases. For example, with a capacity of 2, you could use the following test case:
["LRUCache", "set", "set", "get", "set", "get", "set", "get", "get", "get"]
[[2], [1, 1], [2, 2], [1], [3, 3], [2], [4, 4], [1], [3], [4]]
Output: null, null, null, 1, null, -1, null, -1, 3, 4
Example
Input
["LRUCache", "set", "set", "get", "set", "get", "set", "get", "get", "get"]\n[[2], [1, 1], [2, 2], [1], [3, 3], [2], [4, 4], [1], [3], [4]]\n