3. Longest Substring without repeating characters

Source: Internet
Author: User

Pointers with HashSet

Equivalent to control a small window, if the next letter is a new letter, the small window will move to the right one grid, while checking whether it is the maximum length, if the next letter is duplicated, then let the left side of the small window to the right to the first is not the letter of the place.

Note that the small window to the left of the time, the right side of the small window to move to the right, or the next round of inspection, for the same letter will be checked two times, the front is deleted

1      Public intlengthoflongestsubstring (String s) {2         if(s = =NULL|| S.length () = = 0) {3             return0;4         } 5         intWalker = 0;6         intRunner = 0;7         intMaxLen = 0;8Set<character> set =NewHashset<character>();9          while(Runner <s.length ()) {Ten             Charc =S.charat (runner); One             if(Set.contains (c)) { A                  while(S.charat Walker)! =c) { - Set.remove (S.charat (Walker)); -walker++; the                 } -walker++; -runner++; -}Else { + Set.add (c); -runner++; +MaxLen = (MaxLen < Runner-walker)? (Runner-Walker): MaxLen; A             } at         } -         returnMaxLen; -}

Bug Record:

Made a particularly hidden silly mistake, in the 13th, 14 line of the place, I first walker++, then remove, then deleted the wrong letter .......

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.