1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
package main
import "fmt"
// 寻找最长不含有重复字符的子串(长度)
func maxLenOfNotRepeatString(s string) (maxLength int) {
start := 0
occurred := make(map[rune]int)
// 须将字符串转为[]rune以支持utf8
for i, ch := range []rune(s) {
// 一旦发现有重复,将start位置重置为重复字符的下一位
if lastI, ok := occurred[ch]; ok && lastI >= start {
start = lastI + 1
}
// 检查并调整最大长度
curLength := i - start + 1
if curLength > maxLength {
maxLength = curLength
}
// 记录此字符最后位置
occurred[ch] = i
}
return maxLength
}
func main() {
fmt.Println(maxLenOfNotRepeatString("abcab"))
fmt.Println(maxLenOfNotRepeatString("bbbbb"))
fmt.Println(maxLenOfNotRepeatString("天涯海角"))
fmt.Println(maxLenOfNotRepeatString("芳草萋萋"))
fmt.Println(maxLenOfNotRepeatString("芳草萋萋鹦鹉洲"))
}
|