强行双指针遍历的话会超时55
~
发现自己python学的像坨屎 很忧伤
假设数据[1,2,3,4,2,3] 读到[1,2,3,4],此时新的数据应该为[3,4,2]。
class Solution:
def maxLength(self , arr ):
# write code here
res=list()
maxnum=0
for num in arr:
if num not in res:
res.append(num)
maxnum=max(maxnum,len(res))
else:
res=res[res.index(num)+1:]+[num]
maxnum=max(maxnum,len(res))
return maxnum
方法2:java
list操作中学到了2个东西~
判断是否存在:
list.contais(元素)
切片操作:
不包括右边索引的值~
list=list.subList(1,4)
public class Solution {
public int maxLength (int[] arr) {
// write code here
List list = new ArrayList<>();
int maxnum=0;
for (int num : arr){
if (!list.contains(num)){
list.add(num);
maxnum=Math.max(maxnum,list.size());
}else{
list=list.subList(list.indexOf(num)+1,list.size());
list.add(num);
maxnum=Math.max(maxnum,list.size());
}
}
return maxnum;
}
}
方法3:java
import java.util.*;
public class Solution {
public int maxLength (int[] arr) {
// write code here
Queue queue = new linkedList<>();
// 记录当前无重复元素子数组的长度
int maxLen = 0;
for (int num : arr) {
// 如果队列含有重复元素,队首出队一直到队列没有重复元素
while (queue.contains(num)) {
queue.poll();
}
queue.offer(num);
maxLen = Math.max(maxLen, queue.size());
}
return maxLen;
}
}



