Code Top -- 02 无重复字符的最长子串

无重复字符的最长子串

Posted by OUC_LiuX on March 28, 2022

from CodeTop, Leetcode, 3. 无重复字符的最长子串

给定一个字符串 s ,请你找出其中不含有重复字符的 最长子串 的长度。
典型双指针题目。由于所出现字符都包含在 ASCII 编码内,可以使用静态数组存储字符是实现的次数,节省空间和时间。

经典题,背过即可

int a[256];
int lengthOfLongestSubstring(string s) {
    memset(a, 0, sizeof a);
    int i = 0, j = 0, res = 0;
    while(j < s.size()){
        while(a[s[j]])  a[s[i++]] --;
        a[s[j++]] ++;
        res = max(res, j-i);
    }
    return res;
}