剑指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;
    }
};
posted @ 2020-03-07 19:28  景云ⁿ  阅读(79)  评论(0编辑  收藏  举报