微软件开发面试题解析.docxVIP

  • 0
  • 0
  • 约7.68千字
  • 约 20页
  • 2026-03-23 发布于福建
  • 举报

第PAGE页共NUMPAGES页

2026年微软件开发面试题解析

一、编程实现题(共3题,每题15分,合计45分)

题目1:实现一个简单的LRU缓存机制(15分)

要求:

1.使用Python语言实现LRU(LeastRecentlyUsed)缓存类。

2.缓存容量为固定值,超出容量时,最近最少使用的元素将被移除。

3.实现两个核心方法:`get(key)`和`put(key,value)`,分别用于获取和添加键值对。

4.时间复杂度要求为O(1)。

答案:

python

classLRUCache:

def__init__(self,capacity:int):

self.capacity=capacity

self.cache={}

self.order=[]

defget(self,key:str)-int:

ifkeyinself.cache:

self.order.remove(key)

self.order.append(key)

returnself.cache[key]

return-1

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

ifkeyinself.cache:

self.order.remove(key)

eliflen(self.cache)=se

文档评论(0)

1亿VIP精品文档

相关文档