- 0
- 0
- 约5.82千字
- 约 16页
- 2026-02-11 发布于福建
- 举报
第PAGE页共NUMPAGES页
2026年华为公司技术部门主管面试题及答案解析
一、编程与算法(5题,每题20分,共100分)
1.题目:
编写一个函数,实现快速排序算法,并对以下数组进行排序:`[3,6,8,10,1,2,1]`。要求说明时间复杂度和空间复杂度。
答案:
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=[3,6,8,10,1,2,1]
sorted_arr=quick_sort(arr)
print(sorted_arr)
解析:
快速排序的时间复杂度平均为O(nlogn),最坏情况下为O(n2)。空间复杂度为O(logn),因为递归调用栈的深度决定了空间复杂度。在本题中,数组经过排序后为`[1,1,2,3,6,8,10]`。
2.题目:
给定一个字符串,编写代码删除其中的所有重复字符,并返回新的字符串。例如,输入`abracadabra`,输出`acdr`。
答案:
python
defremove_duplicates(s):
seen=set()
result=[]
forcharins:
ifcharnotinseen:
seen.add(char)
result.append(char)
return.join(result)
s=abracadabra
print(remove_duplicates(s))
解析:
通过使用集合`seen`记录已出现的字符,可以高效地删除重复字符。时间复杂度为O(n),空间复杂度为O(n)。
3.题目:
实现一个LRU(LeastRecentlyUsed)缓存,支持`get`和`put`操作。缓存容量为3,输入操作序列`[1,2,3,4,1,2,3,4,5]`,输出`get`操作的结果。
答案:
python
classLRUCache:
def__init__(self,capacity:int):
self.capacity=capacity
self.cache={}
self.order=[]
defget(self,key:int)-int:
ifkeyinself.cache:
self.order.remove(key)
self.order.append(key)
returnself.cache[key]
return-1
defput(self,key:int,value:int)-None:
ifkeyinself.cache:
self.order.remove(key)
eliflen(self.cache)=self.capacity:
oldest=self.order.pop(0)
delself.cache[oldest]
self.cache[key]=value
self.order.append(key)
lru=LRUCache(3)
operations=[(1,None),(2,None),(3,None),(4,None),(1,None),(2,None),(3,None),(4,None),(5,None)]
forkey,valueinoperations:
ifvalueisnotNone:
lru.put(key,value)
print(lru.get(key))
解析:
LRU缓存通过维护一个双向队列记录访问顺序,`get`操作将元素移到队尾,`put`操作先判断是否超出容量,若超出则删除最久未使用的元素。输出`get`操作结果为`[1,2,3,4,-1]`。
4.题目:
编写一个函数,计算二叉树的最大深度。例如,给定二叉树`[3,9,20,None,None,15,7]`,最大深度为3。
答案:
python
fromcollectionsimportdeque
classTreeNode:
def__init__(self,val=0,left=None,ri
您可能关注的文档
最近下载
- 高效能管理系列工作之PK机制导入.pdf VIP
- 从物、符号构成的世界全域看文艺——赵毅衡教授的“艺术符号论”述评.pdf VIP
- 新世纪大学英语综合教程 (第3版)第1册 PPT课件U1.pptx
- 病历丢失应急预案流程.docx VIP
- 毕业设计(论文)-中型货车驱动桥主减速器设计.doc VIP
- 人大社《社会工作综合能力(初级)》第六章 个案社会工作服务方法 社会工作综合能力(初级).pptx VIP
- 第五章 公共建筑的技术经济问题分析(2).ppt VIP
- 企业员工PK机制--企业激励措施精华.doc VIP
- 跨境破产程序中的数据主权冲突协调机制.docx VIP
- 格劳秀斯:《战争与和平法》.doc VIP
原创力文档

文档评论(0)