LeetCode系列3—无重复字符的最长子串

题意

3. 无重复字符的最长子串

题解

方法一:滑动窗口

class Solution {
    
    
public:
    int lengthOfLongestSubstring(string s) {
    
    
        unordered_set<int> se;
        int n = s.size();
        int right = 0;
        int ans = 0;
        for (int left = 0; left < n; left++) {
    
    
            if (left != 0) se.erase(s[left-1]);
            while (right < n && !se.count(s[right])) {
    
    
                se.insert(s[right]);
                right++;
            }
            ans = max(ans, right - left);
        }
        return ans;
    }
};

Guess you like

Origin blog.csdn.net/younothings/article/details/120321244