C solved(8ms)


  • 0
    H
        int lengthOfLongestSubstring(char* s) {
                 int hashtable[256],j=0;
                 for(;j<256;j++){hashtable[j]=-1;}
                 int index,start=-1;
                 int slenth=strlen(s);        
                 int tmplenth=0;
                 int maxlenth=0;
                 int i;
                 for(i=0;i<slenth;i++)
                 {   index=(int)(*(s+i));
                     if(hashtable[index]==-1)
                            { hashtable[index]=i;
                              tmplenth=i-start;
                              if(tmplenth>maxlenth) maxlenth=tmplenth;
                               }
                    else{
                            if(hashtable[index]<start)
                          /**if the repeated character is in front of the start,ignore it,length plus 1*/
                             { tmplenth=tmplenth+1;//(1)
                               if(tmplenth>maxlenth) maxlenth=tmplenth;
                               hashtable[index]=i;
                                       }
                              else {start=hashtable[index];
                                       hashtable[index]=i;
                                        tmplenth=i-start; }
             /**note the tmplenth is changed,ready for the next (1)*/
                               }
                          }
                       return maxlenth;
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.