剑指OFFER----面试题48. 最长不含重复字符的子字符串

链接:https://leetcode-cn.com/problems/zui-chang-bu-han-zhong-fu-zi-fu-de-zi-zi-fu-chuan-lcof/

代码

class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        unordered_map<char, int> count;
        int res = 0;
        for (int i = 0, j = 0; j < s.size(); ++j) {
            if (++count[s[j]] > 1) {
                while (count[s[i]] == 1) count[s[i++]]--;
                count[s[i++]]--;
            }
            res = max(res, j - i + 1);
        }
        return res;
    }
};

猜你喜欢

转载自www.cnblogs.com/clown9804/p/12436392.html