无重复字符的最长子串

题目:https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/

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
func lengthOfLongestSubstring(s string) int {
sLen := len(s)
if sLen <= 1 {
return sLen
}
tempMap := make([]int, 256) // 字符词频表
right := 0
maxLen := 0
for left := 0; left < sLen; left++ {
for ; right < sLen && tempMap[s[right]] == 0; right++ {
tempMap[s[right]]++
}
fmt.Println(tempMap)
maxLen = max(maxLen, right - left)
if right == sLen {
break
}
tempMap[s[left]]--
}
return maxLen
}

func max(x, y int) int {
if x < y {
return y
}
return x
}

为什么我自己写出来的跟这个性能差这么多,哎, 去抄10遍吧!