题目: 无重复字符的最长子串 给定一个字符串,请你找出其中不含有重复字符的最长子串的长度。 用滑动窗口法求解
class Solution{
public int lengthOfLongestSubstring(String s
){
int len
= s
.length();
int res
= 0;
int start
= 0,end
=0;
Set
<Character>set
= new HashSet<>();
while(start
<len
&&end
<len
){
if(set
.contains(s
.charAt(end
))){
set
.remove(s
.charAt(end
));
start
++;
}else{
set
.add(s
.charAt(end
));
end
++;
res
=Math
.max(res
,end
-start
);
}
}
return res
;
}
}
转载请注明原文地址:https://blackberry.8miu.com/read-27066.html