- 0
- 0
- 约5.33千字
- 约 16页
- 2026-03-05 发布于福建
- 举报
第PAGE页共NUMPAGES页
2026年互联网公司技术部门主管面试题集及解析
一、编程能力与算法设计(共5题,每题10分)
1.题目:
实现一个LRU(最近最少使用)缓存,支持get和put操作。缓存容量为固定值,超出容量时需要淘汰最久未使用的元素。请用Python或Java实现,并说明时间复杂度。
答案与解析:
答案:
python
classLRUCache:
def__init__(self,capacity:int):
self.capacity=capacity
self.cache={}
self.order=collections.OrderedDict()
defget(self,key:int)-int:
ifkeynotinself.cache:
return-1
self.order.move_to_end(key)
returnself.cache[key]
defput(self,key:int,value:int)-None:
ifkeyinself.cache:
self.order.move_to_end(key)
self.cache[key]=value
self.order[key]=None
iflen(self.order)self.capaci
原创力文档

文档评论(0)