【滑动窗口】016. 不含重复字符的最长子字符串

题目链接:https://leetcode-cn.com/problems/wtcaE1/

class Solution {
public:
    int lengthOfLongestSubstring(string s) 
    {
        map<char,int> cntMap;
        int max = 0;
        queue<char> que;
        for (size_t i = 0; i < s.length(); i++) {
            int index = s[i] - 'a';
            cntMap[index]++;
            que.push(s[i]);
            while (cntMap[index] > 1) {
                char top = que.front();
                que.pop();
                cntMap[top - 'a']--;
            }
            max = que.size() > max ? que.size() : max;
        }
        return max;
    }
};

  

posted on 2022-03-26 16:03  蜀山菜鸟  阅读(16)  评论(0)    收藏  举报