腾讯面试题及答案解析.docxVIP

  • 2
  • 0
  • 约3.4万字
  • 约 91页
  • 2026-07-19 发布于河南
  • 举报

腾讯面试题及答案解析

一、数据结构与算法题(30分)

1.请实现一个LRU缓存机制,要求get和put操作的时间复杂度为O(1)。

答案:

LRU(LeastRecentlyUsed)缓存是一种常用的缓存淘汰策略,当缓存满时,会淘汰最近最少使用的数据。要实现时间复杂度为O(1)的LRU缓存,我们可以使用哈希表和双向链表的组合数据结构。

以下是Python实现:

```python

classDLinkedNode:

def__init__(self,key=0,value=0):

self.key=key

self.value=value

self.prev=None

self.next=None

classLRUCache:

def__init__(self,capacity:int):

self.capacity=capacity

self.cache={}

self.head=DLinkedNode()

self.tail=DLinkedNode()

self.head.next=self.tail

self.tail.prev=self.h

文档评论(0)

1亿VIP精品文档

相关文档