leetcode 3. Longest Substring Without Repeating Characters [java]
idea:
设置一个hashset存储非重复元素
j:遍历s
i:最近的一个非重复指针
注意点:
1.Set
2. add remove
public int lengthOfLongestSubstring(String s) {
int i = 0, j = 0, max = 0;
Set<Character> set = new HashSet<>();
while(j < s.length()){
if(! set.contains(s.charAt(j)) ){
set.add(s.charAt(j++));
max = Math.max(max, set.size());
}else{
set.remove(s.charAt(i++));
}
}
return max;
}