题解:
1、找出一个字符串的最长子串,无法避免遍历查询,遍历以每个字符开头的最长子串长度;
2、需要找个哈希表将已经遍历过的缓存起来,新遍历字符已经在哈希表中存在时则为最长长度;
2、删除每次遍历的首字符前面字符缓存;
func lengthOfLongestSubstring(s string) int {
ans, offset := 0, -1
dupMap := make(map[byte]bool, 0)
for i:=0; i<len(s);i++ {
if i>0 {
delete(dupMap, s[i-1])
}
for offset + 1 < len(s) && !dupMap[s[offset + 1]] {
dupMap[s[offset + 1]]= true
offset++
}
ans = func(x,y int)int {
if x> y {
return x
}
return y
}(ans, offset-i+1)
}
return ans
}