The length of the maximum substring in a string that does not contain duplicate letters.
Ideas
1. Using temporary TMP record length, encountering duplicate letters assign the current value of TMP to the length,tmp to return to zero
Compare tmp and length when tmp>length, update length.
2. Each letter is traversed forward to see if there are duplicate letters, using a hash table.
3. Repeated submission of code can not pass after looking at the topic tag, know the need for a double pointer, with a begin point to the beginning of the re-counting, i-begin get the current length,
Each time a repetition is encountered, the begin is assigned to the latter of the sequence number corresponding to the letter of the hash table.
Note: The hash table also updates the ordinal of the repeating letter, otherwise the begin assignment is incorrect each time it is compared to the first occurrence.
"My Code"
intLengthoflongestsubstring (strings) {intLength=s.size (); intL=0, tmp=0; intbegin=0, end=0; Unordered_map<Char,int>Charmap; for(intI=0; i<length; i++){ if(Charmap.find (s[i])! =Charmap.end ()) { if(l<tmp) L=tmp; TMP=i-Begin;//eg:ohommif(begin<charmap[s[i]]+1)//eg:abba begin=charmap[s[i]]+1; } Charmap[s[i]]=i; } if(l<tmp) l=tmp; returnMax (l,length-begin); }
Summary
A bitter tear!
Changed it countless times! The final result is particularly poor ... Crying faint.
See a new solution, a new array, ranked very front, tomorrow carefully analysis.
"Other Code"
intLengthoflongestsubstring (strings) {//Start Typing your/C + + solution below//Do not write int main () functionmemset (Canuse,true,sizeof(Canuse)); intCount =0; intStart =0; intRET =0; for(inti =0; I < s.size (); i++) { if(Canuse[s[i]]) {Canuse[s[i]]=false; Count++; } Else{ret=Max (ret, count); while(true) {Canuse[s[start]]=true; Count--; if(S[start] = =S[i]) Break; Start++; } Start++; Canuse[s[i]]=false; Count++; }} RET=Max (ret, count); returnret; }
OJ Practice 42--t3 longest Substring without repeating characters