- 0
- 0
- 约5.96千字
- 约 19页
- 2026-02-03 发布于福建
- 举报
第PAGE页共NUMPAGES页
2026年微软面试全攻略:高级开发者的考核
1.编程算法题(共5题,每题15分,总分75分)
题目1:
问题描述:
给定一个包含重复元素的整数数组,返回所有不重复的全排列。
要求:
-不能使用递归或内置库函数。
-时间复杂度尽可能低。
示例输入:`[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]))
解析:
-排序去重:首先对数组排序,方便跳过重复元素。
-回溯剪枝:使用`used`数组记录已选择的元素,避免重复。
-跳过重复分支:如果当前元素与上一个元素相同,且上一个元素未被选择,则跳过以避免重复排列。
题目2:
问题描述:
实现一个LRU(最近最少使用)缓存,支持`get`和`put`操作。
要求:
-`get(key)`:返回键对应的值,如果不存在返回-1。
-`put(key,value)`:插入或更新键值对,如果缓存已满,则删除最久未使用的项。
-时间复杂度:`O(1)`。
答案与解析:
python
classListNode:
def__init__(self,key=0,value=0,prev=None,next=None):
self.key=key
self.value=value
self.prev=prev
self.next=next
classLRUCache:
def__init__(self,capacity:int):
self.capacity=capacity
self.cache={}
self.head=ListNode()
self.tail=ListNode()
self.head.next=self.tail
self.tail.prev=self.head
def_add_node(self,node):
node.prev=self.head
node.next=self.head.next
self.head.next.prev=node
self.head.next=node
def_remove_node(self,node):
prev_node=node.prev
next_node=node.next
prev_node.next=next_node
next_node.prev=prev_node
def_move_to_head(self,node):
self._remove_node(node)
self._add_node(node)
def_pop_tail(self):
res=self.tail.prev
self._remove_node(res)
returnres
defget(self,key:int)-int:
ifkeynotinself.cache:
return-1
node=self.cache[key]
self._move_to_head(node)
returnnode.value
defput(self,key:int,value:int)-None:
ifkeyinself.cache:
node=self.cache[key]
node.value=value
self._move_to_head(node)
else:
newNode=ListNode(key,value)
self.cache[key]=newNode
self._a
您可能关注的文档
- 2026年小学语文教师招聘考试模拟试卷含答案.docx
- 2026年制造业运输主管招聘面试题及应答指南.docx
- 2026年餐饮业综合办公室管理面试题集.docx
- 2026年客户反馈分析与问题解决.docx
- 关于怎样顺利通过健康督察考试的技巧分析.docx
- 2026年质量管理体系专员面试题与考试要点.docx
- 市场营销品牌经理面试考核要点.docx
- 绩效考核申诉处理与争议解决含答案.docx
- 2026年软件研发企业采购顾问专业试题详解.docx
- 电力调度员绩效考核与激励机制.docx
- 2026《面向多样性的隐私保护推荐方法概述》7800字.docx
- 2026《基于PLC的智能立体停车场的控制系统设计》7200字.docx
- 2026《太赫兹技术概述》4200字.docx
- 2026《分布式电力系统经济调度研究现状国内外文献综述》2800字.docx
- 2026《曲美家具集团股份有限公司成本管理存在的问题及对策》5600字.docx
- 2026《基于肌肉信号的人体运动研究国内外文献综述》3400字.doc
- 2026《基于城市林学植物配置问题研究》7200字.docx
- 2026《社区重点人群管理平台设计与实现》7900字.docx
- 2026《区县医院内网的设计与实施研究》6900字.doc
- 2026《静电除尘的原理及高频电源的优势综述》3100字.doc
原创力文档

文档评论(0)