- 0
- 0
- 约6.2千字
- 约 19页
- 2026-01-19 发布于福建
- 举报
第PAGE页共NUMPAGES页
2026年IT人才面试常见问题及专业答案
一、编程语言与基础算法(共5题,每题10分,总分50分)
1.题目(Java):
写出Java代码实现一个简单的LRU(最近最少使用)缓存,要求支持get和put操作,并解释时间复杂度。
答案:
java
importjava.util.HashMap;
importjava.util.Map;
publicclassLRUCacheK,V{
privatefinalintcapacity;
privatefinalMapK,Nodecache;
privateNodehead,tail;
privatestaticclassNodeK,V{
Kkey;
Vvalue;
NodeK,Vprev,next;
Node(Kkey,Vvalue){
this.key=key;
this.value=value;
}
}
publicLRUCache(intcapacity){
this.capacity=capacity;
cache=newHashMap();
head=newNode(null,null);
tail=newNode(null,null);
head.next=tail;
tail.prev=head;
}
publicVget(Kkey){
NodeK,Vnode=cache.get(key);
if(node==null)returnnull;
moveToHead(node);
returnnode.value;
}
publicvoidput(Kkey,Vvalue){
NodeK,Vnode=cache.get(key);
if(node!=null){
node.value=value;
moveToHead(node);
}else{
NodeK,VnewNode=newNode(key,value);
cache.put(key,newNode);
addToHead(newNode);
if(cache.size()capacity){
NodeK,VtailNode=removeTail();
cache.remove(tailNode.key);
}
}
}
privatevoidmoveToHead(NodeK,Vnode){
removeNode(node);
addToHead(node);
}
privatevoidaddToHead(NodeK,Vnode){
node.prev=head;
node.next=head.next;
head.next.prev=node;
head.next=node;
}
privatevoidremoveNode(NodeK,Vnode){
node.prev.next=node.next;
node.next.prev=node.prev;
}
privateNodeK,VremoveTail(){
NodeK,Vres=tail.prev;
removeNode(res);
returnres;
}
}
解析:
-使用`HashMap`存储键值对,实现O(1)的get操作。
-使用双向链表维护最近使用顺序,头部为最近使用,尾部为最久未使用。
-put操作时,如果键已存在则更新值并移动到头部;如果超出容量则删除尾部节点。
2.题目(Python):
给定一个数组,返回其中不重复的三元组,使其和为0。例如:输入[-1,0,1,2],输出[[-1,0,1],[-1,2,1]]。
答案:
python
defthreeSum(nums):
nums.sort()
n=len(nums)
res=[]
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
whileleftr
原创力文档

文档评论(0)