3. 无重复字符的最长子串 | LeetCode | Top Interview 150 | Coding Questions
发布: (2026年1月3日 GMT+8 04:57)
1 min read
原文: Dev.to
Source: Dev.to
问题
解法
class Solution {
public int lengthOfLongestSubstring(String s) {
Map map = new HashMap<>();
int left = 0;
int maxLength = 0;
for (int right = 0; right < s.length(); right++) {
char ch = s.charAt(right);
if (map.containsKey(ch)) {
left = Math.max(left, map.get(ch) + 1);
}
map.put(ch, right);
maxLength = Math.max(maxLength, right - left + 1);
}
return maxLength;
}
}