无重复字符的最长子串(leetcode.3)
给定一个字符串 s ,请你找出其中不含有重复字符的 最长子串 的长度。
输入: s = “abcabcbb”
输出: 3
解释: 因为无重复字符的最长子串是 “abc”,所以长度为 3。
class Solution {
public int lengthOfLongestSubstring(String s) {
//也是滑动窗口,不重复时扩充右边界,重复时压缩左边界
Set set = new HashSet();
int len = 0;
int start = 0;
for(int i = 0; i < s.length(); i++){
char ch = s.charAt(i);
if(!set.contains(ch)){
set.add(ch);
}else{
//int end = s.substring(start, s.length()).indexOf(ch);
//while(start <= end){
// set.remove(s.charAt(start++));
//}
while(set.contains(ch)){
set.remove(s.charAt(start));
start++;
}