后端开发面试经验及答案.docxVIP

  • 2
  • 0
  • 约4.89千字
  • 约 14页
  • 2026-03-24 发布于福建
  • 举报

第PAGE页共NUMPAGES页

2026年后端开发面试经验及答案

一、编程题(共3题,每题20分)

要求:请在Python或Java中实现以下功能,并考虑代码的可读性和效率。

1.(20分)实现一个LRU(最近最少使用)缓存机制,支持容量为`capacity`的缓存。缓存应支持`get(key)`和`put(key,value)`操作。当缓存已满时,应移除最久未使用的缓存项。

答案与解析:

python

classLRUCache:

def__init__(self,capacity:int):

self.capacity=capacity

self.cache={}

self.order=[]

defget(self,key:str)-int:

ifkeynotinself.cache:

return-1

self.order.remove(key)

self.order.append(key)

returnself.cache[key]

defput(self,key:str,value:int):

ifkeyinself.order:

self.order.remove(key)

eliflen(self.cache)=self.capacity:

oldest_key=self.or

文档评论(0)

1亿VIP精品文档

相关文档