LeetCode/无重复字符最长子串


给定一个字符串s,请你找出其中不含有重复字符的最长子串的长度。

1. 思路

双指针动态维持更新一个无重复子串,每移动一次右指针,判断新加入字符是否在子串中存在
若存在,则找出其在子串中位置,并更新左指针位置,时间复杂度O(n2)

暴力双循环
class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        int len;
        int maxlen=1;
        if(s.size()==0) return 0;
        for(int i=0,j=1;j

2. 优化

用哈希表维护无重复子串,减少查找,时间复杂度为O(n)

哈希表优化
class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        unordered_set set;// 哈希集合,记录每个字符是否出现过
        int n = s.size();
        // 右指针,初始值为 -1,相当于我们在字符串的左边界的左侧,还没有开始移动
        int rk = -1, ans = 0;
        // 枚举左指针的位置,初始值隐性地表示为 -1
        for (int i = 0; i < n; ++i) {
            if (i != 0) {
                set.erase(s[i - 1]);// 左指针向右移动一格,移除一个字符
            }
            while (rk + 1 < n && !set.count(s[rk + 1])) {
                // 不断地移动右指针
                set.insert(s[rk + 1]);
                ++rk;
            }
            ans = max(ans, rk - i + 1);
        }
        return ans;
    }
};

3. 数组作哈希表

由于字符对应ASCII码一共128位,所以能用一个128大小的布尔数组记录相应字符是否存在于最长子串中
同样也能用128大小的int数组记录查找新加入字符在子串中的位置,用于更新最长子串

动态规划
class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        vector f(128, -1);
        int n = s.size(), start = -1, ans = 0;
        for(int i = 0; i < n; ++i) {
            start = max(start, f[s[i]]);
            ans = max(ans, i-start);
            f[s[i]] = i;
        } 
        return ans;
    }
};

双指针
class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        vector f(128);
        int n = s.size(), ans = 0;
        for(int i = 0, j = 0; j < n; ++j) {
            while(f[s[j]]) f[s[i++]] = false;
            f[s[j]] = true;
            ans = max(ans, j-i+1);
        } 
        return ans;
    }
};