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
// @Title: 不含重复字符的最长子字符串 (不含重复字符的最长子字符串)
// @Author: 15816537946@163.com
// @Date: 2022-03-23 18:08:44
// @Runtime: 8 ms
// @Memory: 2.9 MB
func lengthOfLongestSubstring(s string) int {
	var lo, hi, ret int
	r := make(map[byte]int)
	for lo <= hi && hi < len(s) {
		if cnt, ok := r[s[hi]]; ok && cnt > 0 {
			r[s[lo]]--
			lo++
            continue
		}
        ret = max(ret, hi-lo+1)
		r[s[hi]]++
		hi++
	}

	return ret
}

func max(a,b int) int {
    if a > b {
        return a
    }
    return b
}