天天看点

力扣-3 无重复字符的最长子串Problem DescriptionExampleAC Code(Sliding Window)

Problem Description

力扣-3 无重复字符的最长子串Problem DescriptionExampleAC Code(Sliding Window)

Example

力扣-3 无重复字符的最长子串Problem DescriptionExampleAC Code(Sliding Window)

AC Code(Sliding Window)

class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        if(s.size()==0) return 0;
        int right=0;
        unordered_set<char> map;
        int maxLength=0;
        for(int i=0;i<s.size();i++)
        {
            if(i!=0) map.erase(s[i-1]);
            while(right<s.size()&&!map.count(s[right]))
            {
                map.insert(s[right]);
                right++;
            }
            maxLength=max(maxLength,right-i);
        }
        return maxLength;
    }
};
           
力扣-3 无重复字符的最长子串Problem DescriptionExampleAC Code(Sliding Window)

继续阅读