OJ Practice 42--t3 longest Substring without repeating characters

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.