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
// @Title: 无重复字符的最长子串 (Longest Substring Without Repeating Characters)
// @Author: 15816537946@163.com
// @Date: 2020-08-04 21:54:01
// @Runtime: 0 ms
// @Memory: 2.5 MB
func lengthOfLongestSubstring(s string) int {
	location := [256]int{}
	for i := range location {
		location[i] = -1
	}

	var maxLen, left int

	for i, v := range s {
		if location[v] >= left {
			left = location[v] + 1
		} else if i - left +1 > maxLen {
			maxLen = i - left +1
		}
		location[v] = i
	}
	
	return maxLen

}