[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.

http://blog.csdn.net/pickless/article/details/9018575

#if0classSolution { Public:    intLengthoflongestsubstring (strings) {//Start Typing your/C + + solution below//Do not write int main () function        intlocs[ the];//Save the position of the last occurrence of the charactermemset (locs,-1,sizeof(locs)); intIDX =-1, max =0;//IDX is the starting position of the current substring -1         for(inti =0; I < s.size (); i++)        {            if(Locs[s[i]] > IDX)//if the current character has occurred, the starting position of the current substring is the position of the last occurrence of the character +1{idx=Locs[s[i]]; }            if(I-idx >max) {Max= i-idx; } Locs[s[i]]=i; }        returnMax; }};#endifclassSolution { Public:           intLengthoflongestsubstring (strings) {inthash[ the];//Save the index of s[i]            intStart =-1; intLen =0; memset (Hash,-1,sizeof(hash));  for(inti =0; i< s.size (); i++/**/)            {                   //Update the start if the front of the old Hash[s[i]]                if(Start <Hash[s[i]]) Start=Hash[s[i]]; Len= Max (I-start, Len); Hash[s[i]]=i; }               returnLen; }   };

[Leetcode] 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.