package main
import "fmt"
func lengthOfNonRepeatingSubstr(s string) int{
//int 保存最后出现位置
lastOccurred := make(map[byte]int)
start:=0
maxLength:=0
for i,ch :=range []byte(s){
lastI,ok:=lastOccurred[ch]
if ok && lastI >= start{
start = lastI + 1
}
if i - start + 1 > maxLength {
maxLength = i-start + 1
}
//初始化map
lastOccurred[ch] = i
}
return maxLength
}
func main(){
fmt.Println(lengthOfNonRepeatingSubstr("asdadfghhj"))
}
有疑问加站长微信联系(非本文作者)