美团技术专家面试题解析.docxVIP

  • 0
  • 0
  • 约4.84千字
  • 约 13页
  • 2026-11-02 发布于福建
  • 举报

第PAGE页共NUMPAGES页

2026年美团技术专家面试题解析

一、编程能力测试(共3题,每题20分,总分60分)

1.题目:

实现一个LRU(LeastRecentlyUsed)缓存机制,支持get和put操作。缓存容量为固定值,当缓存满时,需要淘汰最久未使用的元素。请使用Python实现,并说明时间复杂度和空间复杂度。

答案:

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=self.order.pop(0)

delself.cache[oldes

文档评论(0)

1亿VIP精品文档

相关文档