牛课题霸NC41最长无重复子串的长度-Java8题解
题目描述
给定一个数组arr,返回arr的最长无的重复子串的长度(无重复指的是所有数字都不相同)。
方法:双指针+HashSet
import java.util.*;
public class Solution {
/**
*
* @param arr int整型一维数组 the array
* @return int整型
*/
public int maxLength (int[] arr) {
// write code here
int result = 0;
int left = 0, right = 0, len = arr.length;
Set set = new HashSet<>();
for(; right < len; ++right){
int ele = arr[right];
while(set.contains(ele)){
set.remove(arr[left++]);
}
set.add(ele);
result = Math.max(result, right - left + 1);
}
return result;
}
}