3. 无重复字符的最长子串
// 记录出现的索引
func lengthOfLongestSubstring(s string) int {
l, res := 0, 0
count := make(map[rune]int)
for r, val := range s {
index, ok := count[val]
if ok && index >= l{
l = index + 1
}
count[val] = r
res = max(res, r - l + 1)
}
return res
}
// 通用写法
func lengthOfLongestSubstring(s string) int {
l, res := 0, 0
cnt := make(map[rune]int)
for r, val := range s {
cnt[val] ++
for cnt[val] > 1{
cnt[rune(s[l])] --
l ++
}
res = max(res, r - l + 1)
}
return res
}