left=Math.max(left,rightCharindex);有逻辑错误,我将它注释之后用
if(rightCharindex==0){
}else if(left>rightCharindex){
}else {
left=rightCharindex+1;
}
结果反而报错了
问题相关代码,请勿粘贴截图
class Solution {
public int lengthOfLongestSubstring(String s) {
int n=s.length();
int left=0,right=0;
if(n<=1) return n;
int maxLen=1;
Map<Character, Integer> window = new HashMap<>();
while(right<n){
char rightChar = s.charAt(right);
int rightCharindex = window.getOrDefault(rightChar, 0);
//left=Math.max(left,rightCharindex);
if(rightCharindex==0){
}else if(left>rightCharindex){
}else {
left=rightCharindex+1;
}
maxLen=Math.max(maxLen,right - left + 1);
window.put(rightChar,right+1);
right++;
}
return maxLen;
}
}