2026年华为公司技术总监面试题详解及答案.docxVIP

  • 0
  • 0
  • 约6.93千字
  • 约 24页
  • 2026-03-04 发布于福建
  • 举报

2026年华为公司技术总监面试题详解及答案.docx

第PAGE页共NUMPAGES页

2026年华为公司技术总监面试题详解及答案

一、编程与算法(5题,每题20分,共100分)

1.题目:

实现一个函数,输入一个非负整数数组,返回数组中所有可能的三元组,使得三元组的和等于给定的目标值。要求不重复的三元组,并尽可能优化时间复杂度。

答案:

cpp

includevector

includealgorithm

usingnamespacestd;

vectorvectorintthreeSum(vectorintnums,inttarget){

vectorvectorintresult;

if(nums.size()3)returnresult;

sort(nums.begin(),nums.end());

for(inti=0;inums.size()-2;++i){

if(i0nums[i]==nums[i-1])continue;//去重

intleft=i+1,right=nums.size()-1;

while(leftright){

intsum=nums[i]+nums[left]+nums[right];

if(sum==target){

result.push_back({nums[i],nums[left],nums[right]});

while(leftrightnums[left]==nums[left+1])left++;//去重

while(leftrightnums[right]==nums[right-1])right--;//去重

left++;

right--;

}elseif(sumtarget)left++;

elseright--;

}

}

returnresult;

}

解析:

-先对数组排序,便于使用双指针法。

-遍历数组时,跳过重复的元素以避免重复的三元组。

-对于每个固定元素,使用双指针分别从左右两侧查找,满足和等于目标值的三元组。

-时间复杂度:O(n2),空间复杂度:O(1)(不计输出空间)。

2.题目:

给定一个字符串,判断它是否是有效的括号组合(只包含圆括号`()`、方括号`[]`和花括号`{}`)。

答案:

cpp

includestack

includeunordered_map

usingnamespacestd;

boolisValid(strings){

unordered_mapchar,charmapping={{),(},{],[},{},{}};

stackcharst;

for(charc:s){

if(mapping.count(c)){

if(st.empty()||st.top()!=mapping[c])returnfalse;

st.pop();

}else{

st.push(c);

}

}

returnst.empty();

}

解析:

-使用栈存储左括号,遇到右括号时检查栈顶是否匹配。

-若栈为空或栈顶不匹配,则无效。

-最终栈为空则有效。

3.题目:

实现一个LRU(LeastRecentlyUsed)缓存机制,支持`get`和`put`操作。

答案:

cpp

includeunordered_map

includelist

classLRUCache{

public:

LRUCache(intcapacity):capacity(capacity){}

intget(intkey){

autoit=cache.find(key);

if(it==cache.end())return-1;

//将访问的元素移动到链表头部

touch(it-second);

returnit-second-second;

}

voidput(intkey,intvalue){

autoit=cache.find(key);

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

it-second-second=value;//更新值

touch(it-second);

}else{

if(cache.size()==capacity){

cache.erase(lru.back().first);//删除最久未使用的元素

lru.pop_back();

}

lru.emplace_front(key,val

文档评论(0)

1亿VIP精品文档

相关文档