题解 | #最长不含重复字符的子字符串#
最长不含重复字符的子字符串
http://www.nowcoder.com/practice/48d2ff79b8564c40a50fa79f9d5fa9c7
当出现重复字符,以它后面的字符为开始进行初始化
import java.util.*;
public class Solution {
public int lengthOfLongestSubstring (String s) {
// write code here
Set<Character> set = new HashSet<>();
int i,j,ans,t;
char c;
i = j = ans = t = 0;//j表示开头,i表示结尾
while(i < s.length()){
c = s.charAt(i);
if(set.contains(c)){
while(s.charAt(j) != c)set.remove(new Character(s.charAt(j++)));
j++;
t = i-j+1;
} else {
t++;
set.add(c);
}
ans = Math.max(ans,t);
i++;
}
return ans;
}
}