哎,一个滑动窗口,滑动半天,滑的稀里糊涂的。
题目:https://leetcode.cn/problems/longest-substring-without-repeating-characters/
// 滑动窗口
func lengthOfLongestSubstring(s string) int {
var n = len(s)
if n <= 1 { return n }
var maxLen, left, right, window = 1, 0, 0, make(map[byte]bool)
for ;right < n; right++ {
for ;window[s[right]];left++ {
delete(window, s[left])
}
if right - left + 1 > maxLen {
maxLen = right - left + 1
}
window[s[right]] = true
}
return maxLen
}
其他滑动窗口题目:
2 comments
张总,我就问能不能不学了
下次一定 😄