暴力方式
class Solution {
public int lengthOfLongestSubstring(String s) {
Map<Character,Integer> map=new HashMap<>();
int maxLength=0;
int tmpLength=0;
char[] list=s.toCharArray();
for(int i=0;i<list.length;i++){
if(map.get(list[i])==null){
tmpLength++;
map.put(list[i],i);
}else{
i=map.get(list[i]);
map.clear();
tmpLength=0;
}
if(tmpLength>maxLength)
maxLength=tmpLength;
}
return maxLength;
}
}
佛系学习
本文介绍了一种求解字符串中最长无重复字符子串长度的算法实现。通过使用HashMap来跟踪字符及其位置,该算法能够有效地找到最长的不包含重复字符的子串长度。在遍历字符串的过程中,如果遇到重复字符,则更新起始位置并清除之前的记录,确保始终保持最长无重复子串的长度。
692

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



