LeetCode 3. Longest Substring Without Repeating Characters
原题链接
中等
作者:
ChinaPie
,
2021-03-30 01:12:45
,
所有人可见
,
阅读 356
双指针
func lengthOfLongestSubstring(s string) int {
if len(s) == 0{
return 0
}
var freq [256]int
result, left, right := 0, 0, -1
for left < len(s) {
var leftpos = s[left] - 'a'
if right + 1 < len(s) && freq[s[right + 1] - 'a'] == 0 {
freq[s[right + 1] - 'a'] ++
right ++
} else {
freq[leftpos] --
left ++
}
result = max(result, right - left + 1)
}
return result
}
func max(a int, b int) int {
if a > b {
return a
}
return b
}