Files
Hot-100-Algorithm/滑动窗口/无重复字符的最长字串/main.go

38 lines
582 B
Go
Raw Normal View History

2025-08-21 19:04:30 +08:00
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
}