- 0
- 0
- 约6.12千字
- 约 18页
- 2026-11-01 发布于福建
- 举报
第PAGE页共NUMPAGES页
2026年美团高级工程师面试题及答案
一、编程能力(5题,每题20分,共100分)
1.题目(20分):
实现一个LRU(LeastRecentlyUsed)缓存,支持get和put操作。缓存容量为固定值,当访问或插入元素导致缓存已满时,需要淘汰最久未使用的元素。请用Python或Java实现,并说明时间复杂度。
答案:
python
classLRUCache:
def__init__(self,capacity:int):
self.capacity=capacity
self.cache={}
self.order=[]
defget(self,key:int)-int:
ifkeyinself.cache:
self.order.remove(key)
self.order.append(key)
returnself.cache[key]
return-1
defput(self,key:int,value:int)-None:
ifkeyinself.cache:
self.order.remove(key)
eliflen(self.cache)==self.capacity:
oldest_key=self.order.pop(0)
dels
原创力文档

文档评论(0)