无重复字符的最长子串

题目链接:https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/
题目描述:

题解:


class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        if(s.size() < 2)
            return s.size();
        unordered_set<char> window;
        int maxLen = 0;
        int left = 0;
        for(char i = 0; i < s.size(); i++)
        {
            while(window.find(s[i]) != window.end())
            {
                window.erase(s[left]);
                left++;
            }
            maxLen = max(maxLen, i - left + 1);
            window.insert(s[i]);
        }

        return maxLen;
    }
};

posted @ 2021-06-20 17:51  张宵  阅读(19)  评论(0编辑  收藏  举报