面试题四十八:最长不含重复字符的子字符串
int Max_Str(String s){ char [] str=s.toCharArray(); int curlength=0;//子串长度 int maxlength=0; int []B=new int[26]; for(int i=0;i<26;i++) B[i]=-1; for(int i=0;i<str.length;i++) { int temp=B[str[i]-'a']; if(temp<0||i-temp>curlength) //temp》=0表示该字符是重复的,那么就得判断是不是在当前匹配的子字符串中重复的 //如果当前子串长度小于两个重复字符的距离,那么不用管 curlength++; else { if(curlength>maxlength) maxlength=curlength curlength=i-temp; //排除<u>temp</u>前的;表示新字串从<u>temp</u>+1开始 //如<u>asdfsr</u>;s重复,从<u>dfs</u>赋予新长度 } B[str[i]-'a']=i;//将当前下标保存更新 } if(curlength>maxlength) //在结束前更新长度;防止遗漏 //因为防止一直没有重复导致上面判断没有更新 maxlength=curlength; return maxlength; }
浪波激泥