思想
1: 固定左边界, 移动右边界, 每次遇到重复字符, 开始计算最长字串
2: 左边界开始向左移动, 直到消掉重复字符,然后执行1
代码
func lengthOfLongestSubstring(s string) int {
length := len(s)
mapStr := make(map[byte]int, 0)
start := 0
end := 0
longnum := 0
for start < length {
for ; end < length; end++{
mapStr[s[end]]++
if v, _:= mapStr[s[end]]; v == 2{
break
}
}
if longnum < end - start {
longnum = end -start
}
i := start
for ;i < end; i++ {
mapStr[s[i]]--
if mapStr[s[i]] == 1{
break
}
}
start = i +1
end++
}
return longnum
}