2026年IT行业面试技术难题及答案.docxVIP

  • 0
  • 0
  • 约4.79千字
  • 约 15页
  • 2026-01-21 发布于福建
  • 举报

第PAGE页共NUMPAGES页

2026年IT行业面试技术难题及答案

一、编程语言与数据结构(共5题,每题10分)

1.题目:

请用Python实现一个函数,输入一个非空字符串,返回该字符串中第一个只出现一次的字符。如果不存在,则返回空格。

答案:

python

deffirst_unique_char(s:str)-str:

count={}

forcharins:

count[char]=count.get(char,0)+1

forcharins:

ifcount[char]==1:

returnchar

return

解析:

首先使用字典统计每个字符的出现次数,然后遍历字符串,返回第一个计数为1的字符。时间复杂度为O(n),空间复杂度为O(m),其中m为字符集大小。

2.题目:

给定一个包含重复元素的数组,请返回所有不重复的三元组,使得三元组的和等于给定的数。例如,输入`[1,-2,-5,0,3]`和`0`,输出`[[-2,0,2],[-5,1,4]]`。

答案:

python

defthree_sum(nums:list)-list:

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`操作。缓存容量为3,例如:

-`put(1,1)`:缓存为`{1:1}`

-`put(2,2)`:缓存为`{1:1,2:2}`

-`get(1)`:返回`1`,缓存更新为`{2:2,1:1,3:3}`

-`put(3,3)`:缓存为`{1:1,2:2,3:3}`

-`put(4,4)`:删除最久未使用的`2`,缓存为`{1:1,3:3,4:4}`

答案:

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`操作先删除最久未使用的键(如果超出容量),然后插入新键值对。

4.题目:

请用C++实现一个二叉树的层序遍历(广度优先遍历),返回结果为二维数组。例如:

输入:[3,9,20,null,null,15,7]

输出:[[3],[9,20],[15,7]]

答案:

cpp

includevector

includequeue

usingnamespacestd;

structTreeNode{

文档评论(0)

1亿VIP精品文档

相关文档