- 2
- 0
- 约5.87千字
- 约 17页
- 2026-04-08 发布于福建
- 举报
第PAGE页共NUMPAGES页
2026年腾讯高级开发工程师面试全解析及答案
一、编程基础(5题,每题10分,共50分)
1.题目:
请实现一个函数,输入一个正整数`n`,返回`n`的二进制表示中`1`的个数。要求时间复杂度为O(1)。
答案:
cpp
intcountBits(intn){
intcount=0;
while(n){
count+=n1;
n=1;
}
returncount;
}
解析:
该方法利用位运算,每次将`n`右移一位,并统计最低位的`1`的数量。时间复杂度为O(1),因为二进制位数固定(32位或64位)。
2.题目:
给定一个链表,判断其是否为回文链表。要求空间复杂度为O(1)。
答案:
cpp
boolisPalindrome(ListNodehead){
if(!head||!head-next)returntrue;
ListNodeslow=head,fast=head;
while(fast-nextfast-next-next){
slow=slow-next;
fast=fast-next-next;
}
ListNodeprev=nullptr,next=nullptr;
while(slow){
nex
原创力文档

文档评论(0)