Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "ABCABCBB" are "abc", which the length is 3. For "bbbbb" the longest substring are "B", with the length of 1.
http://blog.csdn.net/pickless/article/details/9018575
#if0classSolution { Public: intLengthoflongestsubstring (strings) {//Start Typing your/C + + solution below//Do not write int main () function intlocs[ the];//Save the position of the last occurrence of the charactermemset (locs,-1,sizeof(locs)); intIDX =-1, max =0;//IDX is the starting position of the current substring -1 for(inti =0; I < s.size (); i++) { if(Locs[s[i]] > IDX)//if the current character has occurred, the starting position of the current substring is the position of the last occurrence of the character +1{idx=Locs[s[i]]; } if(I-idx >max) {Max= i-idx; } Locs[s[i]]=i; } returnMax; }};#endifclassSolution { Public: intLengthoflongestsubstring (strings) {inthash[ the];//Save the index of s[i] intStart =-1; intLen =0; memset (Hash,-1,sizeof(hash)); for(inti =0; i< s.size (); i++/**/) { //Update the start if the front of the old Hash[s[i]] if(Start <Hash[s[i]]) Start=Hash[s[i]]; Len= Max (I-start, Len); Hash[s[i]]=i; } returnLen; } };
[Leetcode] longest Substring without repeating characters