Loading

【LBLD】我写了首诗,把滑动窗口算法算法变成了默写题

【LBLD】我写了首诗,把滑动窗口算法算法变成了默写题

76.最小覆盖子串

class Solution {
public:
    string minWindow(string s, string t) {
        unordered_map<char, int> need, window;
        for (char c : t) need[c]++;

        int left=0, right=0;
        int valid=0;
        int start=0, len=INT_MAX;
        while (right < s.size()) {
            char c = s[right];
            right++;
            if (need.count(c)) {
                window[c]++;
                if (need[c] == window[c]) {
                    valid++;
                }
            }

            while (valid == need.size()) {
                if (right - left < len) {
                    start = left;
                    len = right - left;
                }
                char d = s[left];
                left++;
                if (need.count(d)) {
                    if (window[d] == need[d]) {
                        valid--;
                    }
                    window[d]--;
                }
            }
        }
        return len == INT_MAX ? "" : s.substr(start, len);
    }
};

567. 字符串的排列

class Solution {
public:
    bool checkInclusion(string s1, string s2) {
        unordered_map<char, int> need, window;
        for(char c: s1) need[c]++;
        
        int left=0, right=0;
        int valid=0;
        while (right < s2.size()) {
            char c = s2[right];
            right++;

            if (need.count(c)) {
                window[c]++;
                if (window[c] == need[c]) {
                    valid++;
                }
            }

            while (right - left >= s1.size()) {
                if (valid == need.size()) {
                    return true;
                }

                char d = s2[left];
                left++;

                if (need.count(d)) {
                    if (window[d] == need[d]) {
                        valid--;
                    }
                    window[d]--;
                }
            }
        }
        return false;
    }
};

438. 找到字符串中所有字母异位词

class Solution {
public:
    vector<int> findAnagrams(string s, string p) {
        unordered_map<int, int> need, window;
        for (char c: p) need[c]++;
        
        int left = 0, right = 0;
        vector<int> res;
        int valid = 0;
        while (right < s.size()) {
            char c = s[right];
            right++;
            
            if (need.count(c)) {
                window[c]++;
                if (need[c] == window[c]) {
                    valid++;
                }
            }

            while (right - left >= p.size()) {
                if (valid == need.size()) {
                    res.push_back(left);
                }

                char d = s[left];
                left++;

                if (need.count(d)) {
                    if (need[d] == window[d]) {
                        valid--;
                    }
                    window[d]--;
                }
            }
        }
        return res;
    }
};

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

class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        unordered_map<char, int> window;

        int left = 0, right = 0;
        int len = 0;
        while (right < s.size()) {
            char c = s[right];
            right++;
            
            window[c]++;
            
            while (window[c] == 2) {
                if (len < right - left - 1) len = right - left - 1;
                char d = s[left];
                left++;

                if (window.count(d)) {
                    window[d]--;
                }
            }
        }
        return len > right - left ? len : right - left;
    }
};
posted @ 2023-04-12 20:42  杨谖之  阅读(23)  评论(0编辑  收藏  举报