题目:
思路:
双指针,窗口内字符放入HashSet中。
代码:
java
public int lengthOfLongestSubstring(String s) {
int start = 0, end = 0;
int max = 0;
Set<Character> set = new HashSet<>();
while (start < s.length() && end < s.length() && start <= end) {
if (set.contains(s.charAt(end))) {
set.remove(s.charAt(start));
start ++;
} else {
set.add(s.charAt(end));
max = Math.max(max, end - start + 1);
end ++;
}
}
return max;