滑动窗口

最长无重复字母子串

using namespace std;
class solution{
public:
    int lengthOfLongestSubstring(string s)
    {
        int freq[256] ={0};
        int l = 0; r = -1; //滑动窗口为s[l....r]
        int res = 0;
        while(l

leetcode438

public:
    vector findAnagrams(string s, string p) {
        vector ans;
        if(p.empty() || s.empty()) return ans;

        int cnt[256] = {0};
        for(char ch : p) cnt[ch] ++;
        
        int lf = 0, rt = 0;
        while(rt < s.size()) {
            cnt[s[rt]] --;
            while(lf <= rt && cnt[s[rt]] < 0) {
                cnt[s[lf ++]] ++;
            }
            if(rt - lf + 1 == p.size()) {
                ans.push_back(lf);
                cnt[s[lf ++]] ++;
            }
            rt ++;
        }
        return ans;
    }

你可能感兴趣的:(滑动窗口)