联想集团研发岗位面试题及答案参考.docxVIP

  • 0
  • 0
  • 约7.52千字
  • 约 20页
  • 2026-03-08 发布于福建
  • 举报

联想集团研发岗位面试题及答案参考.docx

第PAGE页共NUMPAGES页

2026年联想集团研发岗位面试题及答案参考

一、编程能力测试(共5题,每题10分,总分50分)

1.题目:请编写一个函数,实现快速排序算法,并对以下数组进行排序:`[12,4,5,23,1,56,34]`。

要求:使用Python语言,输出排序后的数组。

答案:

python

defquick_sort(arr):

iflen(arr)=1:

returnarr

pivot=arr[len(arr)//2]

left=[xforxinarrifxpivot]

middle=[xforxinarrifx==pivot]

right=[xforxinarrifxpivot]

returnquick_sort(left)+middle+quick_sort(right)

arr=[12,4,5,23,1,56,34]

sorted_arr=quick_sort(arr)

print(sorted_arr)

解析:快速排序是一种分治算法,通过选取一个基准值(pivot),将数组分为小于、等于、大于基准值的三部分,然后递归地对左右两部分进行排序。上述代码中,基准值选择为数组的中间值,然后分别对左右子数组进行递归排序,最终合并结果。

2.题目:请编写一个Java方法,实现二叉树的深度优先遍历(前序遍历),并输出遍历结果。

要求:使用递归方式实现,二叉树结构如下:

java

classTreeNode{

intval;

TreeNodeleft;

TreeNoderight;

TreeNode(intx){val=x;}

}

答案:

java

classSolution{

publicvoidpreorderTraversal(TreeNoderoot){

if(root==null){

return;

}

System.out.print(root.val+);

preorderTraversal(root.left);

preorderTraversal(root.right);

}

}

解析:前序遍历的顺序是根节点、左子树、右子树。递归实现时,首先访问当前节点,然后递归遍历左子树,最后递归遍历右子树。上述代码中,`preorderTraversal`方法首先判断当前节点是否为空,如果不为空,则输出节点值,并递归遍历左右子树。

3.题目:请用C++实现一个简单的LRU(最近最少使用)缓存,支持`get`和`put`操作。

要求:缓存容量为3,输出每次操作后的缓存状态。

答案:

cpp

includeiostream

includeunordered_map

includelist

classLRUCache{

public:

LRUCache(intcapacity):capacity_(capacity){}

intget(intkey){

autoit=cache_map.find(key);

if(it==cache_map.end()){

return-1;

}

//Movetofront

cache_list.splice(cache_list.begin(),cache_list,it-second);

returnit-second-second;

}

voidput(intkey,intvalue){

autoit=cache_map.find(key);

if(it!=cache_map.end()){

//Updatevalueandmovetofront

it-second-second=value;

cache_list.splice(cache_list.begin(),cache_list,it-second);

}else{

if(cache_map.size()==capacity_){

//Removeleastrecentlyused

cache_map.erase(cache_list.back().first);

cache_list.pop_back();

}

cache_list.push_front({key,value});

cache_map[key]=cache_list.begin();

}

}

private:

intcapacity_;

std::liststd::pairint,intcache_list;//(key,valu

文档评论(0)

1亿VIP精品文档

相关文档