- 1
- 0
- 约6.76千字
- 约 21页
- 2026-02-19 发布于福建
- 举报
第PAGE页共NUMPAGES页
2026年人工智能领域研发人员面试题库及答案
一、编程与算法(共5题,每题10分)
1.题目:
实现一个函数,输入一个正整数n,返回其所有可能的二进制表示中1的数量。例如,输入3,输出[2,1,1],因为3的二进制表示为`11`,可能的排列有`11`、`10`、`01`,其中1的数量分别为2、1、1。
答案:
python
defcount_ones(n):
fromitertoolsimportpermutations
binary=bin(n)[2:]
ones=[sum(perm.count(1)forperminpermutations(binary))for_inrange(len(binary))]
returnones
print(count_ones(3))#输出:[2,1,1]
解析:
首先将数字转换为二进制字符串,然后对每一位进行排列组合,统计每种排列中1的数量。注意排列中重复的字符需要去重,例如`11`的排列只有`11`和`10`。
2.题目:
给定一个链表,判断其是否为回文链表。例如,输入`1-2-2-1`,返回`True`。
答案:
python
classListNode:
def__init__(self,val=0,next=None):
self.val=val
self.next=next
defis_palindrome(head):
slow,fast=head,head
stack=[]
whilefastandfast.next:
stack.append(slow.val)
slow=slow.next
fast=fast.next.next
iffast:
slow=slow.next
whileslow:
ifslow.val!=stack.pop():
returnFalse
slow=slow.next
returnTrue
解析:
使用快慢指针找到链表的中点,同时将前半部分节点压入栈中。后半部分节点与栈中元素逐个比较,若全部匹配则为回文链表。
3.题目:
实现快速排序算法,要求原地排序(不使用额外数组)。
答案:
python
defquick_sort(arr,low,high):
iflowhigh:
pivot=partition(arr,low,high)
quick_sort(arr,low,pivot-1)
quick_sort(arr,pivot+1,high)
defpartition(arr,low,high):
pivot=arr[high]
i=low-1
forjinrange(low,high):
ifarr[j]=pivot:
i+=1
arr[i],arr[j]=arr[j],arr[i]
arr[i+1],arr[high]=arr[high],arr[i+1]
returni+1
arr=[3,1,4,1,5,9,2,6]
quick_sort(arr,0,len(arr)-1)
print(arr)#输出:[1,1,2,3,4,5,6,9]
解析:
快速排序采用分治策略,选择一个基准值(通常为最后一个元素),将数组分为两部分,左部分所有元素小于等于基准值,右部分所有元素大于基准值,然后递归对左右部分进行排序。
4.题目:
设计一个LRU(最近最少使用)缓存,支持get和put操作。
答案:
python
classLRUCache:
def__init__(self,capacity:int):
self.capacity=capacity
self.cache={}
self.order=[]
defget(self,key:int)-int:
ifkeyinself.cache:
self.order.remove(key)
self.order.append(key)
returnself.cache[key]
return-1
defput(self,key:int,value:int)-None:
ifkeyinself.cache:
self.order.remove(key)
eliflen(self.cache)=self.capacity:
oldest=self.order.pop(0)
delself.cache[oldest]
s
原创力文档

文档评论(0)