LeetCode刷题系列 第一题 两个字符串最长重复数据
废话不多说,直接上代码
public static int lengthOfLongestSubstring(String s) {
if (s == null || s.isEmpty()) {
return 0;
}
int strSize = s.length();
Set<Character> charSet = new HashSet<>();
int maxLength = 0;
int left = 0;
int right = 0;
while (right < strSize) {
if (!charSet.contains(s.charAt(right))) {
charSet.add(s.charAt(right));
maxLength = Math.max(maxLength, right - left + 1);
System.out.println("maxLength = " + maxLength);
right++;
} else {
charSet.remove(s.charAt(left));
left++;
}
}
return maxLength;
}