题解 | #最长不含重复字符的子字符串#
最长不含重复字符的子字符串
http://www.nowcoder.com/practice/48d2ff79b8564c40a50fa79f9d5fa9c7
初学go,发现没人分享golang的解法,分享一下,大佬勿喷
双指针
func lengthOfLongestSubstring(s string) int {
count := make(map[string]int)
maxLength := 0
r := 0
n := len(s)
for l :=0;l<n;l++{
for r<n{
if _, ok :=count[string(s[r])];!ok{
count[string(s[r])] = 1
r += 1
}else {
break
}
}
if r-l>maxLength{
maxLength = r-l
}
count[string(s[l])] -= 1
if count[string(s[l])] == 0{
delete(count, string(s[l]))
}
}
return maxLength
}