- 0
- 0
- 约7.24千字
- 约 20页
- 2026-03-09 发布于福建
- 举报
第PAGE页共NUMPAGES页
2026年腾讯技术部面试攻略与答案详解
一、编程能力测试(共5题,每题10分,总分50分)
题目1(Python编程,10分):
编写一个函数,实现将输入的字符串按照每个单词的首字母进行排序,如果首字母相同,则保持原顺序。例如,输入`applebananaorangeapple`,输出`appleapplebananaorange`。要求不使用内置排序函数,手动实现排序逻辑。
答案与解析:
python
defsort_by_first_letter(s):
words=s.split()
sorted_words=[]
forwordinwords:
ifnotsorted_words:
sorted_words.append(word)
else:
inserted=False
foriinrange(len(sorted_words)):
ifword[0]sorted_words[i][0]:
sorted_words.insert(i,word)
inserted=True
break
ifnotinserted:
sorted_words.append(word)
return.join(sorted_words)
解析:
1.分割字符串:使用`split()`按空格分割字符串,得到单词列表。
2.手动排序:遍历每个单词,通过首字母与已排序列表的每个首字母比较,找到合适位置插入,保持稳定性。
3.时间复杂度:O(n2),适用于小规模输入;可优化为平衡树结构(如AVL树)实现O(nlogn)。
题目2(Java编程,10分):
实现一个无重复数字的快速随机数生成器,要求生成1-100之间的整数,且每个数字被生成的概率相同。禁止使用`Random`类,需手动实现。
答案与解析:
java
importjava.util.;
publicclassFastRandomGenerator{
privateint[]numbers;
privateintindex;
privateRandomrand;
publicFastRandomGenerator(){
numbers=newint[100];
for(inti=0;i100;i++){
numbers[i]=i+1;
}
rand=newRandom();
index=100;
}
publicintnextInt(){
if(index==0)thrownewNoSuchElementException(Nomorenumbersavailable);
intrandomIdx=rand.nextInt(index);
intresult=numbers[randomIdx];
numbers[randomIdx]=numbers[index-1];
index--;
returnresult;
}
publicstaticvoidmain(String[]args){
FastRandomGeneratorgen=newFastRandomGenerator();
for(inti=0;i10;i++){
System.out.println(gen.nextInt());
}
}
}
解析:
1.Fisher-Yates洗牌算法:通过原地交换数组元素实现均匀随机。
2.空间复杂度:O(1),额外存储100个数字;时间复杂度:O(1)每次调用。
3.腾讯倾向:考察算法思维与无内置库实现能力。
题目3(C++编程,10分):
给定一个包含重复元素的数组,返回所有可能的子集(无重复子集)。例如,输入`[1,2,2]`,输出`[[],[1],[1,2],[1,2,2],[2],[2,2]]`。
答案与解析:
cpp
includevector
includealgorithm
usingnamespacestd;
classSolution{
public:
vectorvectorintsubsetsWithDup(vectorintnums){
vectorvectorintresult;
sort(nums.begin(),nums.end());
backtrack(nums,0,vectorint(),result);
returnresult;
}
private:
voidbacktrack(vectorintnums,intstart,v
您可能关注的文档
最近下载
- 四年级科学下册3保护土壤资源冀人版.pptx VIP
- Yamaha雅马哈双簧管英文用户手册.pdf
- 学习雷锋好榜样歌词拼音注音版.docx
- 新晃事业单位考试真题卷.pdf VIP
- 2017款长城哈弗H2s蓝标_汽车使用手册用户操作图解驾驶车主车辆说明书电子版.pdf
- 北京德合伟业最新表面处理技术在电厂的应用20190109(邯郸)精简.pdf VIP
- 计算机系统原理13015习题答案.pdf VIP
- 上海市静安区2026届初三一模语文试题(含答案).pdf
- KEYENCE基恩士CV-X 系列 安装手册 [面型相机篇].pdf VIP
- 统编版语文七年级下册第16课《有为有不为》(教学课件).pptx VIP
原创力文档

文档评论(0)