3. 无重复字符的最长子串

  • 哈希表中 (s[r]) 的计数加一, 同时将指针 (r) 向后移一位
  • 不断向后移动 (l),直至区间 ([l,r-1])(s[r-1]) 的个数等于1为止;
class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        int n=s.size();
        unordered_map<char,int> mp;
        int l=0,r=0;
        int res=0;
        while(r < n)
        {
            char c=s[r];
            mp[c]++;
            r++;

            while(mp[c] > 1)
                mp[s[l++]]--;
            res=max(res,r-l);
        }
        return res;
    }
};
原文地址:https://www.cnblogs.com/fxh0707/p/14572200.html