"Leetcode" "longest Substring without repeating characters" "the oldest string of non-repeating characters" "C + +"

Source: Internet
Author: User

  • Title: Given a string, the length of the oldest string with no repeating character is obtained.
  • Idea: For loop once, time complexity is O (N). The ASCII value of the character is 32~126. Start represents the initial position of the current non-repeating character string, with an initial value of 0, and a positional array of pos[128] that represents the position of the corresponding character at the current position of the for Loop index. If the current character is S[i] (its ASCII value is Cur_pos), if Pos[cur_pos]>=start, indicating that the character S[i] after start, the substring starting with start encounters a repeating character for the first time, stop. Determine whether the current length is greater than Max_len, or update max_len if it is greater. Then update start, and Pos[cur_pos] .... Note the processing of the last character.
  • Code:
    classSolution { Public:    intLengthoflongestsubstring (strings) {intlen_s=s.size (); Const intm= -; intPos[m];  for(intI=0; i<m;i++) Pos[i]=-1; intstart=0; intmax_len=0;  for(intI=0; i<len_s;i++)        {            intcur_pos=S[i]; if(pos[cur_pos]>=start) {                if(max_len< (istart)) {Max_len=i-start; } Start=pos[cur_pos]+1; } Pos[cur_pos]=i; }        if((Len_s-start) >Max_len) Max_len=len_s-start; returnMax_len; }};

"Leetcode" "longest Substring without repeating characters" "the oldest string of non-repeating characters" "C + +"

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.