Leetcode-Question-146: LRU Cache

原文链接:
https://leetcode.com/problems/lru-cache/

题目描述:
LRU Cache
Difficulty: 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(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.

解答:

另一种折腾:

注意看缓存是否满的时候用的是map的size取法,而非list的,提交时发现后者的时间消耗是前者的2倍。有人说是某些c++版本list.size()操作非O(1)复杂度。

发表评论

电子邮件地址不会被公开。 必填项已用*标注