添加新题目
This commit is contained in:
37
滑动窗口/无重复字符的最长字串/main.go
Normal file
37
滑动窗口/无重复字符的最长字串/main.go
Normal file
@@ -0,0 +1,37 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user