2026年IT面试流程及问题解析.docxVIP

  • 0
  • 0
  • 约7.72千字
  • 约 24页
  • 2026-01-19 发布于福建
  • 举报

第PAGE页共NUMPAGES页

2026年IT面试流程及问题解析

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

1.题目:

请实现一个函数,输入一个正整数n,输出所有可能的二进制字符串,且每个字符串中0和1的数量相同。例如,当n=2时,输出[01,10]。

答案:

python

defbinary_strings(n):

ifn==0:

return[]

ifn==1:

return[0,1]

prev=binary_strings(n-1)

result=[]

forsinprev:

result.append(s+0)

result.append(s+1)

returnresult[:len(result)//2]

print(binary_strings(2))#输出:[01,10]

解析:

通过递归方式生成所有可能的二进制字符串,然后筛选出0和1数量相同的字符串。关键在于理解递归的终止条件和如何避免重复。

2.题目:

给定一个包含重复数字的数组,请返回所有不重复的全排列。例如,输入[1,1,2],输出[[1,1,2],[1,2,1],[2,1,1]]。

答案:

python

defpermute_unique(nums):

defbacktrack(path,used,res):

iflen(path)==len(nums):

res.append(path.copy())

return

foriinrange(len(nums)):

ifused[i]:

continue

ifi0andnums[i]==nums[i-1]andnotused[i-1]:

continue

used[i]=True

path.append(nums[i])

backtrack(path,used,res)

path.pop()

used[i]=False

nums.sort()

res=[]

used=[False]len(nums)

backtrack([],used,res)

returnres

print(permute_unique([1,1,2]))#输出:[[1,1,2],[1,2,1],[2,1,1]]

解析:

通过回溯法生成全排列,关键在于如何处理重复元素。通过排序和跳过相邻重复元素来避免重复排列。

3.题目:

请实现一个LRU缓存机制,支持get和put操作。get(key)返回key对应的值,如果不存在返回-1;put(key,value)将key和value存入缓存,如果缓存已满则删除最久未使用的项。

答案:

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(2)

lru.put(1,1)

lru.put(2,2)

print(lru.get(1))#返回1

lru.put(3,3)#去除键2

print(lru.get(2))#返回-1

解析:

使用哈希表存储键值对,双向链表维护访问顺序。get操作将键移到链表尾部,put操作先删除最久未使用的键,再添加新键。

4.题目:

请实现一个函数,输入一个字符串,输出所有可能的子集。例如,输入abc,输出[,a,b,c,ab,ac,bc,abc]。

答案:

python

defsubsets(s:str):

s=sorted(s)

res=[]

defbacktrack(index,path):

res.append(.jo

文档评论(0)

1亿VIP精品文档

相关文档