哈希
class Solution{
public int lengthOfLongestSubstring(String s){
int Max=0,left=0,right=0;
char[] charArray=s.toCharArray();
Set<Character> set=new HashSet<>();
for(int i=0;i<s.length();i++){
while(set.contains(charArray[i]))
set.remove(charArray[left++]);
set.add(charArray[i]);
int temp=right-left+1;
right++;
Max=Max>temp?Max:temp;
}
return Max;
}
}
该博客介绍了一个求解字符串中最长无重复字符子串的算法。通过使用哈希集合来跟踪已出现的字符,当遇到重复字符时,从哈希集合中移除最左侧的字符,更新最长子串长度。算法在遍历字符串过程中维护了最长无重复子串的动态信息。
867

被折叠的 条评论
为什么被折叠?



