自学内容网 自学内容网

算法——滑动窗口

滑动窗口


定长窗口:
class Solution {
public:
    int maxVowels(string s, int k) {
        int cnt = 0, res = -1;
        int i = 0, j = -1;
        while(j < (int)s.size() - 1) {
            j ++;
            if(s[j] == 'a' || s[j] == 'e' || s[j] == 'i' || s[j] == 'o' || s[j] == 'u') {
                cnt ++;
            }
            if(j - i + 1 == k) {
                res = max(res, cnt);
                if(s[i] == 'a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'o' || s[i] == 'u') {
                    cnt --;
                }
                i ++;
            }
        }
        return res;
    }
};

原文地址:https://blog.csdn.net/Doctor_Anonymous/article/details/144017067

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!