阿里巴国际交易技术岗位的面试要点与答案.docxVIP

  • 0
  • 0
  • 约7.91千字
  • 约 24页
  • 2026-02-09 发布于福建
  • 举报

阿里巴国际交易技术岗位的面试要点与答案.docx

第PAGE页共NUMPAGES页

2026年阿里巴国际交易技术岗位的面试要点与答案

一、编程与算法题(共5题,每题10分,总分50分)

1.题目:

请实现一个函数,输入一个字符串,返回该字符串中不重复字符的最长子串长度。例如,输入`abcabcbb`,输出`3`(最长不重复子串为`abc`)。

答案:

java

publicintlengthOfLongestSubstring(Strings){

int[]charIndex=newint[128];//ASCII字符集

Arrays.fill(charIndex,-1);

intmaxLength=0,start=-1;

for(inti=0;is.length();i++){

charc=s.charAt(i);

if(charIndex[c]start){

start=charIndex[c];

}

charIndex[c]=i;

maxLength=Math.max(maxLength,i-start);

}

returnmaxLength;

}

解析:

使用滑动窗口+哈希表。`charIndex`数组记录每个字符最后出现的位置,`start`表示当前窗口的起始位置。当字符重复时,更新`start`为重复字符的下一个位置。时间复杂度O(n),空间复杂度O(1)。

2.题目:

设计一个LRU(LeastRecentlyUsed)缓存,支持`get`和`put`操作。缓存容量为`capacity`,当访问或插入超出容量时,删除最久未使用的数据。

答案:

java

classLRUCache{

classNode{

intkey,value;

Nodeprev,next;

Node(intk,intv){key=k;value=v;}

}

MapInteger,Nodecache=newHashMap();

Nodehead,tail;

intcapacity;

publicLRUCache(intcapacity){

this.capacity=capacity;

head=newNode(0,0);

tail=newNode(0,0);

head.next=tail;

tail.prev=head;

}

publicintget(intkey){

if(cache.containsKey(key)){

Nodenode=cache.get(key);

moveToHead(node);

returnnode.value;

}

return-1;

}

publicvoidput(intkey,intvalue){

if(cache.containsKey(key)){

Nodenode=cache.get(key);

node.value=value;

moveToHead(node);

}else{

if(cache.size()==capacity){

cache.remove(tail.prev.key);

removeNode(tail.prev);

}

NodenewNode=newNode(key,value);

cache.put(key,newNode);

addNode(newNode);

}

}

privatevoidmoveToHead(Nodenode){

removeNode(node);

addNode(node);

}

privatevoidaddNode(Nodenode){

node.prev=head;

node.next=head.next;

head.next.prev=node;

head.next=node;

}

privatevoidremoveNode(Nodenode){

node.prev.next=node.next;

node.next.prev=node.prev;

}

}

解析:

使用双向链表+哈希表实现。双向链表维护访问顺序,哈希表实现O(1)访问。`get`操作将节点移动到头部,`put`操作先判断是否超出容量,若超出则删除尾节点。

3.题目:

给定一个数组`nums`和一个目标值`target`,返回所有和为`target`的`nums`的索引组合。例如,`nums=[2,3,6,7]`,`target=7`,输出`[[0,1],[2,3]]`。

答案:

java

publicLis

文档评论(0)

1亿VIP精品文档

相关文档