- 0
- 0
- 约6.56千字
- 约 21页
- 2026-01-19 发布于福建
- 举报
第PAGE页共NUMPAGES页
2026年IT行业软件工程师的面试题目集
一、编程基础与数据结构(共5题,每题10分)
1.题目:
请实现一个函数,输入一个非负整数`n`,返回`n`的二进制表示中`1`的个数。例如,输入`11`(二进制为`1011`),返回`3`。
答案:
python
defcount_bits(n):
count=0
whilen:
count+=n1
n=1
returncount
解析:
利用位运算,每次右移一位并检查最低位是否为`1`,直到`n`为`0`。时间复杂度为O(logn)。
2.题目:
给定一个链表,判断是否为回文链表。例如,`1-2-2-1`是回文链表。
答案:
python
classListNode:
def__init__(self,val=0,next=None):
self.val=val
self.next=next
defis_palindrome(head):
ifnothead:
returnTrue
slow=fast=head
whilefastandfast.next:
slow=slow.next
fast=fast.next.next
stack=[]
whileslow:
stack.append(slow.val)
slow=slow.next
whilestack:
ifstack.pop()!=head.val:
returnFalse
head=head.next
returnTrue
解析:
快慢指针找到中点,后半部分入栈,然后与前半部分对比。时间复杂度O(n),空间复杂度O(n)。
3.题目:
实现一个LRU(最近最少使用)缓存,支持`get`和`put`操作。
答案:
python
classLRUCache:
def__init__(self,capacity:int):
self.capacity=capacity
self.cache={}
self.head,self.tail=Node(0,0),Node(0,0)
self.head.next=self.tail
self.tail.prev=self.head
defget(self,key:int)-int:
ifkeynotinself.cache:
return-1
node=self.cache[key]
self._remove(node)
self._add(node)
returnnode.value
defput(self,key:int,value:int)-None:
ifkeyinself.cache:
self._remove(self.cache[key])
node=Node(key,value)
self.cache[key]=node
self._add(node)
iflen(self.cache)self.capacity:
lru=self.tail.prev
self._remove(lru)
delself.cache[lru.key]
def_remove(self,node):
delself.cache[node.key]
node.prev.next=node.next
node.next.prev=node.prev
def_add(self,node):
node.next=self.head.next
node.next.prev=node
node.prev=self.head
self.head.next=node
解析:
使用双向链表+哈希表实现。链表维护访问顺序,哈希表实现O(1)访问。
4.题目:
给定一个数组,找出其中和为`target`的两个数,并返回它们的索引。例如,`nums=[2,7,11,15]`,`target=9`,返回`[0,1]`。
答案:
python
deftwo_sum(nums,target):
hash_map={}
fori,numinenumerate(nums):
complement=target-num
ifcomplementinhash_map:
return[hash_map[complement],i]
hash_map[num]=i
return[]
解析:
哈希表记录每个数的索引,时间复杂
您可能关注的文档
最近下载
- 深度解析(2026)《JBT 6888-2018风机用铸钢件 技术条件》.pptx VIP
- PEP人教版小学六年级上册英语(语法填空练习试题)含答案(共3套).pdf
- 众为兴TV5600-B01(DJ8849V1)系列点胶控制系统用户手册.pdf VIP
- BYK内部助剂培训资料[宣讲].ppt VIP
- 2022年全国森林草原湿地调查监测技术方案.pdf VIP
- ZZ032 建筑信息模型建模赛题第9套-2023年全国职业院校技能大赛赛项赛题 .pdf VIP
- 机电篇——地下室机电安装施工与BIM技术应用.ppt VIP
- 11774.3-2014 数控重型曲轴车床 第3部分.pdf VIP
- 文旅融合理念的价值维度与乡村文化振兴实践.docx VIP
- 产业园区物业服务运营规范.docx
原创力文档

文档评论(0)