-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathT3.java
More file actions
28 lines (26 loc) · 767 Bytes
/
T3.java
File metadata and controls
28 lines (26 loc) · 767 Bytes
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
import java.util.HashSet;
class Solution {
public int lengthOfLongestSubstring(String s) {
//双指针
//哈希表查存在否
//哈希集合也可以
//map里只能存包装类
HashSet<Character> set = new HashSet<>();
int res = 0;
//
for(int i = 0,j = -1;i < s.length();i++){
if(set.contains(s.charAt(i))){
res = Math.max(res, set.size());
while (set.contains(s.charAt(i))) {
j++;
set.remove(s.charAt(j));
}
set.add(s.charAt(i));
}else{
set.add(s.charAt(i));
}
}
res = Math.max(res, set.size());
return res;
}
}