- 解决问题之前,我们要知道什么是子串。子串指的是给定数组中连续的一段。对比子序列就可以很好地明白,子序列可以是不连续的。
class Solution {
public int lengthOfLongestSubstring(String s) {
int n = s.length();
Set set = new HashSet<>();
int result=0;
int i=0;
int j=0;
while(i<n && j<n){
if(!set.contains(s.charAt(j))){
set.add(s.charAt(j));
j++;
result=Math.max(result,j-i);
}
else{
set.remove(s.charAt(i));
i++;
}
}
return result;
}
}