- 0
- 0
- 约6.47千字
- 约 18页
- 2026-01-21 发布于福建
- 举报
第PAGE页共NUMPAGES页
2026年IT行业技术岗位面试常见问题解析
一、编程与算法(共5题,每题10分,总分50分)
1.题目:请实现一个函数,输入一个非空字符串,返回字符串中第一个只出现一次的字符。如果不存在,返回空格。例如,输入abaccdeff,返回b。
答案:
python
deffirst_unique_char(s):
count={}
forcharins:
count[char]=count.get(char,0)+1
forcharins:
ifcount[char]==1:
returnchar
return
解析:
-使用字典记录每个字符的出现次数,遍历一次字符串统计频率。
-再次遍历字符串,返回第一个频率为1的字符。
-时间复杂度O(n),空间复杂度O(1)(假设字符集固定)。
2.题目:给定一个整数数组,判断数组中是否存在三个元素a,b,c,使得a+b+c=0。请找出所有不重复的三元组。
答案:
python
defthree_sum(nums):
nums.sort()
res=[]
n=len(nums)
foriinrange(n):
ifi0andnums[i]==nums[i-1]:
continue
left,right=i+1,n-1
whileleftright:
total=nums[i]+nums[left]+nums[right]
iftotal==0:
res.append([nums[i],nums[left],nums[right]])
whileleftrightandnums[left]==nums[left+1]:
left+=1
whileleftrightandnums[right]==nums[right-1]:
right-=1
left+=1
right-=1
eliftotal0:
left+=1
else:
right-=1
returnres
解析:
-先排序数组,避免重复解。
-固定一个数,使用双指针法查找另外两个数。
-时间复杂度O(n2),空间复杂度O(1)。
3.题目:请实现一个LRU(最近最少使用)缓存,支持get和put操作。缓存容量为capacity。
答案:
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)
解析:
-使用字典存储键值对,列表维护访问顺序。
-get时将键移到列表末尾,put时先删除最久未使用的键(如果超出容量)。
-时间复杂度O(1),空间复杂度O(capacity)。
4.题目:给定一个二叉树,判断其是否是完全二叉树。完全二叉树是指除最后一层外,每一层节点都尽可能满,且最后一层节点都集中在左侧。
答案:
python
defis_complete_binary_tree(root):
ifnotroot:
returnTrue
queue=[root]
end=False
whilequeue:
node=queue.pop(0)
ifend:
ifnode:
returnFalse
else:
ifnode:
queue.append(node.left)
queue.append(node.right)
else:
end=True
returnTrue
解析:
-层序遍历二叉树,使用标志位标记是否遇到过空节点。
-如果遇到空节点后还有非空节点,则不是完全二叉树。
-时
原创力文档

文档评论(0)