- 0
- 0
- 约7.12千字
- 约 20页
- 2026-11-06 发布于福建
- 举报
第PAGE页共NUMPAGES页
2026年腾讯技术部面试官经验与答案解析
一、编程能力(5题,共20分)
1.(4分)编写一个函数,输入一个正整数n,返回一个列表,其中包含从1到n(包含n)的所有奇数。
答案:
python
defodd_numbers(n):
return[iforiinrange(1,n+1)ifi%2!=0]
解析:
-使用列表推导式生成奇数列表,`range(1,n+1)`生成1到n的整数,`ifi%2!=0`筛选奇数。
-时间复杂度O(n),空间复杂度O(n)。
2.(4分)实现一个简单的LRU(LeastRecentlyUsed)缓存,支持`get(key)`和`put(key,value)`操作。
答案:
python
classLRUCache:
def__init__(self,capacity):
self.cache={}
self.capacity=capacity
self.order=[]
defget(self,key):
ifkeyinself.cache:
self.order.remove(key)
self.order.append(key)
returnself.cache[key]
return-1
defput(self
原创力文档

文档评论(0)