- 2
- 0
- 约6.4千字
- 约 19页
- 2026-05-17 发布于福建
- 举报
第PAGE页共NUMPAGES页
2026年滴出行软件开发面试题库
一、编程能力测试(共5题,每题10分)
题目1:数据结构实现(10分)
请用Java或Python实现一个LRU(LeastRecentlyUsed)缓存系统,要求:
1.支持get(key)操作,返回键对应的值,如果不存在返回-1
2.支持put(key,value)操作,如果缓存容量已满,需要驱逐最久未使用的元素
3.缓存容量为固定值,例如3
参考答案:
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)-None:
ifkeyinself.cache:
self.order.remove(key)
eliflen(self.cache)=self.capacity
原创力文档

文档评论(0)