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

This problem is meant to find the oldest string in a given character that does not contain repeating characters.

The algorithm is not difficult, where additional space is required to record whether each character has been present, as well as a head pointer p and a tail pointer of Q.

Each time the loop moves backward Q, if S[Q] does not appear, it is marked as appearing, otherwise found in the [P,q] range s[q] The first occurrence is labeled T, p to T is marked as not appearing, then P is updated to t+1, (do not forget to mark the character currently being processed) until all strings have been processed.

In fact, I've come a detour here. The first pass of the code takes 148ms, ranked in the second half of the C + + distribution, the code is as follows:

classSolution { Public:    intLengthoflongestsubstring (strings) {stringT; BOOLhasappeared[ -]; intMax =0, _pos; if(s.size () = =0|| S.size () = =1)            returns.size ();  for(inti =0; I < -; i++)            * (hasappeared + i) =false;  for(inti =0; I < s.size (); i++) {            if(!Hasappeared[s[i]]) {Hasappeared[s[i]]=true; T+=S[i]; }            Else{_pos=T.find (S[i]); Max= Max < T.size ()?t.size (): Max;  for(intj =0; J <= _pos; J + +) Hasappeared[t[j]]=false; T= T.substr (_pos +1); T+=S[i]; Hasappeared[s[i]]=true; }} Max= Max < T.size ()?t.size (): Max; returnMax; }};

Because you are not familiar with C + +, it is not clear that some APIs are implemented. My guess is that the substr method takes up more time. Maybe it's a copy of the original string. At that time still think this kind of writing seems to be more elegant.

Think of a half-day thinking should be correct, so swapped with the pointer (subscript), the code is as follows:

classSolution { Public:    intLengthoflongestsubstring (strings) {BOOLhasappeared[ -]; intI, max =0, _pos, _st =0; if(s.size () = =0|| S.size () = =1)            returns.size ();  for(i =0; I < -; i++)            * (hasappeared + i) =false;  for(i =0; I < s.size (); i++) {            if(!Hasappeared[s[i]]) {Hasappeared[s[i]]=true; }            Else{_pos=S.find (S[i], _st); Max= Max < I-_st? I_st:max;  for(intj = _st; J <= _pos; J + +) Hasappeared[s[j]]=false; _st= _pos +1; Hasappeared[s[i]]=true; }} Max= Max < I-_st? I_st:max; returnMax; }};

So the running time is shortened to 48ms, is basically the best. Every time I am confused, the front of those running time close to 0 and faster than the mathematical optimal solution of what is going on. The tangle of useless, continue to learn.

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