- 1
- 0
- 约6.86千字
- 约 19页
- 2026-10-19 发布于福建
- 举报
第PAGE页共NUMPAGES页
2026年百度搜索算法工程师面试问题集
一、编程基础与数据结构(共5题,每题8分)
1.题目:给定一个未排序的整数数组,返回其中出现次数超过阈值(例如50%)的最小整数。要求时间复杂度O(n),空间复杂度O(1)。
示例:输入`[3,4,3,2,2,3,3]`,阈值50%,输出`3`。
答案:
-使用摩尔投票法(Boyer-MooreMajorityVote):
1.初始化`candidate=None`,`count=0`;
2.遍历数组:
-若`count==0`,则`candidate=num`,`count=1`;
-否则,若`num==candidate`,`count+=1`;否则`count-=1`;
3.验证`candidate`是否满足阈值(可通过第二次遍历统计频率);
-代码示例(Python):
python
defmajority_element(nums,threshold=50):
candidate=None
count=0
fornuminnums:
ifcount==0:
candidate=num
count=1
elifnum==candidate:
count+=1
else:
count
原创力文档

文档评论(0)