-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLengthOfLongestSubstring.java
More file actions
40 lines (36 loc) · 1.02 KB
/
Copy pathLengthOfLongestSubstring.java
File metadata and controls
40 lines (36 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package LeetCode;
import java.util.HashSet;
import java.util.Set;
/**
* @FileName: LengthOfLongestSubstring.java
* @Description: 无重复字符的最长子串
* @Author: ABCpril
* @Date: 2021/11/29
*/
public class LengthOfLongestSubstring {
public int lengthOfLongestSubstring(String s) {
if (s.length() < 2) {
return s.length();
}
Set<Character> window = new HashSet<>();
int l = 0, r = -1, res = 0;
char[] chars = s.toCharArray();
while (r < s.length() - 1) {
++r;
if (!window.contains(chars[r])) {
window.add(chars[r]);
}
else {
// when window needs shrink
while (chars[l] != chars[r]) {
window.remove(chars[l]);
++l;
}
// l此时指向与rightChar相同的字符,需再后移一位
++l;
}
res = Math.max(res, r - l + 1);
}
return res;
}
}