Leetcode 3. Longest Substring without repeating characters

Source: Internet
Author: User

The initial idea, and the simplest idea, is to maintain a string res, preserving the current non-repeating string. A bit of string s is read in, read into each bit and the res is compared, without repetition to join Res; if repeated, Res retains only the portion of the char to the right of the s[i] duplicate, plus s[i] to update the maxlen before the res is intercepted. When string s is finished, update the MaxLen again, because there may be no duplicate characters for the entire string s.

1 classSolution {2  Public:3     intLengthoflongestsubstring (strings) {4         intMaxLen =0;5         stringres ="";6          for(inti =0; I < s.length (); ++i) {7             if(res = ="") Res + =S[i];8             Else{9                 BOOLFlag =true;Ten                 intpos =0; One                  for(intj =0; J < Res.length (); ++j) { A                     if(S[i] = =Res[j]) { -Flag =false; -pos =J; the                          Break; -                     } -                 } -                 if(flag) Res + =S[i]; +                 Else{ -                     intLen =res.length (); +MaxLen =Max (MaxLen, Len); Ares = RES.SUBSTR (pos+1, Res.length ()-1-POS) +S[i]; at                 } -             } -         } -         intLen =res.length (); -         returnMax (MaxLen, Len); -     } in};

The second idea, in fact, stems from the first, but added the data type of map, this thought can reduce the time, but more slowly.

Hash holds each char in string s and its corresponding position, if Char appears more than once, the position is the most recent occurrence.

Maintains two variables, L, R, representing the left and right positions (including relationships, [L,r]) of the string without duplicates.

Each read into S[r], if already in the hash, update l,l = max (L, Hash[s[r]] + 1); Explain, suppose and s[r] the position of s[i],s[i] may be on the left side of s[l] (e.g. " TMMT "2nd T reads, l=2, and s[i] = t, i = 0), and possibly to the right of S[l], then L jumps to the right of s[i] to ensure that no char repeats from L to R.

Each read into S[r] Remember to update the hash s[r] corresponding to the location, but also to update the value of MaxLen. (You can also update the value of MaxLen each time you encounter a repeating char, but it's different.)

1 classSolution {2  Public:3     intLengthoflongestsubstring (strings) {4map<Char,int>Hash;5         intL =0, r =0;6         intMaxLen =0;7          while(R <s.length ()) {8             if(Hash.find (s[r])! =Hash.end ()) {9L = max (L, Hash[s[r]] +1);Ten             } OneHash[s[r]] =R; AMaxLen = Max (MaxLen, R-l +1); -++R; -         } the         returnMaxLen; -     } -};

The third kind, DP, should be the fastest, stay the pit.

Https://leetcode.com/discuss/90845/my-6ms-java-solution-beating-85%25-with-explanation

Leetcode 3. 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.