2026年腾讯研发工程师面试常见问题集.docxVIP

  • 0
  • 0
  • 约1.52万字
  • 约 35页
  • 2026-02-12 发布于福建
  • 举报

2026年腾讯研发工程师面试常见问题集.docx

第PAGE页共NUMPAGES页

2026年腾讯研发工程师面试常见问题集

一、编程基础与数据结构(共5题,每题10分,总分50分)

题目1:字符串反转

问题描述:实现一个函数,将输入的字符串反转。例如,输入腾讯科技,输出科技术腾。

答案:

cpp

includestring

usingnamespacestd;

stringreverseString(strings){

intleft=0,right=s.size()-1;

while(leftright){

swap(s[left],s[right]);

left++;

right--;

}

returns;

}

解析:采用双指针法,时间复杂度O(n),空间复杂度O(1)。也可以使用递归实现,但空间复杂度会增加。

题目2:合并两个有序链表

问题描述:合并两个有序链表,合并后的链表保持有序。例如,链表1为1-3-5,链表2为2-4-6,合并后为1-2-3-4-5-6。

答案:

cpp

structListNode{

intval;

ListNodenext;

ListNode(intx):val(x),next(NULL){}

};

ListNodemergeTwoLists(ListNodel1,ListNodel2){

ListNodedummy(0);

ListNodetail=dummy;

while(l1l2){

if(l1-vall2-val){

tail-next=l1;

l1=l1-next;

}else{

tail-next=l2;

l2=l2-next;

}

tail=tail-next;

}

tail-next=l1?l1:l2;

returndummy.next;

}

解析:使用虚拟头节点简化边界处理,时间复杂度O(n),空间复杂度O(1)。

题目3:查找数组中的重复元素

问题描述:给定一个包含n+1个整数的数组,其中每个整数都在1到n之间,找出数组中的重复元素。例如,数组[1,3,4,2,2]中重复元素为2。

答案:

cpp

includevector

usingnamespacestd;

intfindDuplicate(vectorintnums){

intslow=nums[0];

intfast=nums[nums[0]];

while(slow!=fast){

slow=nums[slow];

fast=nums[nums[fast]];

}

fast=0;

while(slow!=fast){

slow=nums[slow];

fast=nums[fast];

}

returnslow;

}

解析:采用Floyd判圈算法(龟兔赛跑),时间复杂度O(n),空间复杂度O(1)。

题目4:二叉树的最大深度

问题描述:给定一个二叉树,返回其最大深度。例如,树[3,9,20,null,null,15,7]的最大深度为3。

答案:

cpp

includevector

usingnamespacestd;

structTreeNode{

intval;

TreeNodeleft;

TreeNoderight;

TreeNode(intx):val(x),left(NULL),right(NULL){}

};

intmaxDepth(TreeNoderoot){

if(!root)return0;

return1+max(maxDepth(root-left),maxDepth(root-right));

}

解析:递归计算左右子树深度,取最大值加1。时间复杂度O(n),空间复杂度O(h)。

题目5:拓扑排序

问题描述:给定一个有向无环图,返回一个拓扑排序的顶点序列。例如,图[[0,1],[1,2],[1,3],[3,3]]的拓扑排序可以为[0,1,2,3]。

答案:

cpp

includevector

includequeue

usingnamespacestd;

vectorinttopologicalSort(intnumCourses,vectorvectorintprerequisites){

vectorvectorintadj(numCourses,vectorint());

vectorintinDegree(numCourses,0);

for(constautopre:prerequisites){

adj[pre[1]].push_back(pr

文档评论(0)

1亿VIP精品文档

相关文档