题目:
3. Longest Substring Without Repeating Characters(medium)
解题思路:
滑动窗口典型例题
代码:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23public int lengthOfLongestSubstring(String s) {
if(s == null || s.length() == 0) return 0;
HashMap<Character,Integer> window = new HashMap<>();
int max = 0;
int left = 0,right = 0;
while(right < s.length()){
char c = s.charAt(right);
right ++;
window.put(c,window.getOrDefault(c,0)+1);
System.out.println("window:["+left+","+right+")");
while(window.get(c) > 1){//不含重复字符
char d = s.charAt(left);
left++;
window.put(d,window.getOrDefault(d,0)-1);
}
max = Math.max(max,right - left);
}
return max;
}