38 lines
582 B
Go
38 lines
582 B
Go
package main
|
|
|
|
import "fmt"
|
|
|
|
func main() {
|
|
fmt.Println(lengthOfLongestSubstring(" "))
|
|
}
|
|
|
|
func lengthOfLongestSubstring(s string) int {
|
|
if len(s) == 0 {
|
|
fmt.Println("直接")
|
|
return 0
|
|
}
|
|
if len(s) == 1 {
|
|
return 1
|
|
}
|
|
left, right := 0, 1
|
|
window := 1
|
|
result := 0
|
|
hash := make(map[rune]struct{}, 0)
|
|
hash[rune(s[0])] = struct{}{}
|
|
for right < len(s) {
|
|
if _, ok := hash[rune(s[right])]; ok {
|
|
window--
|
|
delete(hash, rune(s[left]))
|
|
left++
|
|
|
|
} else {
|
|
hash[rune(s[right])] = struct{}{}
|
|
right++
|
|
window++
|
|
result = max(result, window)
|
|
}
|
|
|
|
}
|
|
return result
|
|
}
|