funclengthOfLongestSubstring(s string)int { // byte与其index,如果重复取最大的index覆盖 var mp = make(map[byte]int) var left, max = 0, 0
for i := 0; i < len(s); i++ { length := i - left + 1 if _, ok := mp[s[i]]; ok { length2 := i - mp[s[i]] // 如果left+1在mp[s[i]]左边,则更新left指针到mp[s[i]]+1 if left-1 < mp[s[i]] { length = length2 left = mp[s[i]] + 1 } } if length > max { max = length } mp[s[i]] = i } // 结尾的字符串 iflen(s)-left > max { max = len(s) - left }