- 1
- 0
- 约5.06千字
- 约 15页
- 2026-02-10 发布于福建
- 举报
第PAGE页共NUMPAGES页
2026年阿里巴集团面试题及答案
一、编程题(共3题,每题20分,总分60分)
1.题目(20分):
编写一个函数,实现LRU(最近最少使用)缓存机制。缓存容量为固定值,当缓存满时,需要淘汰最久未使用的元素。要求使用链表和哈希表实现,并给出Python代码。
答案:
python
classListNode:
def__init__(self,key=0,value=0,prev=None,next=None):
self.key=key
self.value=value
self.prev=prev
self.next=next
classLRUCache:
def__init__(self,capacity:int):
self.capacity=capacity
self.cache={}
self.head=ListNode(0,0)
self.tail=ListNode(0,0)
self.head.next=self.tail
self.tail.prev=self.head
defget(self,key:int)-int:
ifkeyinself.cache:
node=self.cache[key]
self._move_to_he
原创力文档

文档评论(0)