LeetCode 剑指 Offer 48. 最长不含重复字符的子字符串

题目描述

剑指 Offer 48. 最长不含重复字符的子字符串

image-20241107211629480

思路分析

思路描述

见:LeetCode 3. 无重复字符的最长子串

参考代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
func lengthOfLongestSubstring(s string) int {
	charIndex := map[byte]int{} // 记录字符上一次出现的位置
	res := 0
	left := 0

	for right := 0; right < len(s); right++ {
		cur := s[right]
		// 如果当前字符在窗口中,更新 left
		if pre, ok := charIndex[cur]; ok && pre >= left {
			left = pre + 1
		}
		// 更新字符最新位置
		charIndex[cur] = right

		res = max(res, right-left+1)
	}
	return res
}
1
write your code here

➡️ 点击查看 Java 题解

1
write your code here
本文作者:
本文链接: https://hgnulb.github.io/blog/2023/94004328
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议,转载请注明出处!