[Leetcode] Longest Substring without repeating characters

Source: Internet
Author: User

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.

Thinking of solving problems

Set the left and right two pointers, and the left hand pointer is not duplicated, and the correct pointer moves to the right, otherwise it moves to the right (until there are no repeating characters), and the maximum number of characters is counted.

Implementation code
//rumtime:60 MSclassSolution { Public:intLengthoflongestsubstring (strings) { Set<int>ResintMaxLen =0;intleft =0;intI for(i =0; I < s.size (); i++) {if(Res.find (s[i])! = Res.end ()) {maxlen = max (MaxLen, i-left); while(S[left]! = S[i])                {res.erase (s[left++]);            } left++; }Else{Res.insert (s[i]); }} maxlen = Max (MaxLen, i-left);returnMaxLen; }};

[Leetcode] Longest Substring without repeating characters

Related Article

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.