Likou 3. The longest substring without repeated characters---sliding window + hash table

3. The longest substring without repeated characters

Given a string, please find out the length of the longest substring that does not contain repeated characters.

Example 1:

Input: s = "abcabcbb"
Output: 3
Explanation: Because the longest substring without repeated characters is "abc", its length is 3.
Example 2:

Input: s = "bbbbb"
Output: 1
Explanation: Because the longest substring without repeated characters is "b", its length is 1.
Example 3:

Input: s = "pwwkew"
Output: 3
Explanation: Because the longest substring without repeated characters is "wke", its length is 3.
Please note that your answer must be the length of the substring, "pwke" is a subsequence, not a substring.
Example 4:

Input: s = ""
Output: 0

提示:
0 <= s.length <= 5 * 10^4
s 由英文字母、数字、符号和空格组成

answer:

Method: sliding window + hash table

The main idea is to use the hash table idea to complete the operation of judging whether there will be repeated characters in the substring , which can greatly reduce the amount of code. That is, we first form a substring from the head of the original array, and then every time a character is added to him, we must first determine whether the added character has appeared in the original substring. If it does not look too is a plus and then continue, if there have been at this time it is needed by the idea of a sliding window , a sliding our substring beginning at index and at this time to be added corresponding to the character repeat that The subscript corresponding to the character plus one . Then calculate the length of the originally formed substring. After the substring start index is updated, the part before the start index of the hash table that we serve as a counter must be updated again . Then continue the above operation later.
Icon:
Insert picture description here

Note that in order to facilitate the formation of a new starting subscript each time, we define the value stored in the hash table as the corresponding subscript of the character in the original array.

Detailed visible code:

Code:

#define max(a,b) ((a)>(b)?(a):(b))
int lengthOfLongestSubstring(char * s){
    
    
    int hah[128];//哈希表
    memset(hah,-1,128*sizeof(int));//先都初始化为-1
    int num = 0;
    int start = 0;//开始的起点
    int num2 = 1;
    int num3 = 0;
    int x;
    if(strlen(s)==0)
        return 0;
    if(strlen(s)==1)
        return 1;
    for(int i=0;i<strlen(s);i++)
    {
    
    
        if(hah[s[i]]==-1)//一旦还没有碰到该字符s[i]时
        {
    
    
            hah[s[i]]=i;//储存对应元素下标
            num3++;
            if(i==strlen(s)-1)//这是为了避免一直到末尾都不用更新开始下标的情况,即避免了“aab”类情况
            //即可能会漏算最后形成的一个子串的长度,所以我们将这个长度与前面那些子串长度进行比较
            {
    
    
                x=i-start+1;
                return max(x,num2);
            }
        }
        else//一旦哈希表碰到了两次同样的字符时
        {
    
    
            num = i-1-start+1;//计算一下原先形成的子串长度
            num2 = max(num2,num);//储存更大的子串长度
            start = hah[s[i]]+1;//更新子串开始下标
            for(int j=1;j<128;j++)
            {
    
    
                if(hah[j]<start)//将哈希表中每个字符储存的下标在新开始下标前面的部分的元素都初始化为原先的样子
                {
    
    
                    hah[j]=-1;
                }
            }
            hah[s[i]]=i;//原先这个没有计数,现在给他计数一下,因为他是包含在新形成的子串里的
        }
    }
    if(num3==strlen(s))//若一直没有更新子串开始下标,即直接本身数组便是答案的情况
    {
    
    
        return num3;
    }
    else
    {
    
    
        return num2;
    }
}

Guess you like

Origin blog.csdn.net/xiangguang_fight/article/details/112389551