• 专题二:滑动窗口【优选算法】


    滑动窗口

    什么时候用? 同向双指针(找单调性)

    怎么用?

    1)用left、right指针维护窗口

    2)进窗口(right指针++,更新窗口内的值)

    3)判断

            出窗口(left++,并更新窗口内的值)

    4)更新结果(注意每道题更新结果的时机不同,需具体分析)

    目录

    滑动窗口:

    1、长度最小的子数组

    2、无重复字符的最长子串

    3、最大连续1的个数 

    4、将x减到0的最小操作数

    5、水果成篮

     6、找到字符串中所有字母异位词

    7、串联所有单词的子串(容器的使用)

    8、最小覆盖子串 


    1、长度最小的子数组

    1. class Solution {
    2. public:
    3. int minSubArrayLen(int target, vector<int>& nums) {
    4. int n = nums.size();
    5. int sum = 0,len = INT_MAX;
    6. for(int l = 0,r = 0;r < n;r++){
    7. sum += nums[r];//入窗口
    8. while(sum >= target)//判断
    9. {
    10. len = min(len,r - l + 1);//更新结果
    11. sum -= nums[l++];//出窗口
    12. }
    13. }
    14. return len == INT_MAX?0:len;
    15. }
    16. };

    2、无重复字符的最长子串

     暴力解法,枚举每一个字串,但分析可发现,出现重复字符时,left可直接跳到重复字符,right也无需往回走。因此就是同向双指针,也就是滑动窗口。

    1. class Solution {
    2. public:
    3. int lengthOfLongestSubstring(string s) {
    4. vector<int> hash(128);//用来判断字符是否出现过
    5. int len = 0,n = s.size();
    6. for(int l = 0,r = 0;r < n;r++){//进窗口
    7. hash[s[r]]++;
    8. while(hash[s[r]] > 1)//判断
    9. {
    10. hash[s[l]]--;//出窗口
    11. l++;
    12. }
    13. len = max(len,r-l+1);//更新结果
    14. }
    15. return len;
    16. }
    17. };

    3、最大连续1的个数 

    1. class Solution {
    2. public:
    3. int longestOnes(vector<int>& nums, int k) {
    4. int n = nums.size();
    5. int count = 0;
    6. int ret = 0;
    7. for(int left = 0,right = 0;right < n;right++){//进窗口
    8. if(nums[right] == 0) count++;
    9. while(count > k)//判断
    10. if(nums[left++] == 0)count--;//出窗口
    11. ret = max(ret,right-left+1);//更新结果
    12. }
    13. return ret;
    14. }
    15. };

    4、将x减到0的最小操作数

     

    通过正难则反,可以将此题转换成类似第一题 

    1. class Solution {
    2. public:
    3. int minOperations(vector<int>& nums, int x) {
    4. int n = nums.size();
    5. int sum = 0;
    6. for(auto x : nums){
    7. sum += x;
    8. }
    9. int target = sum - x;
    10. if(target < 0) return -1;
    11. int ret = -1,sum2 = 0;
    12. for(int left = 0,right = 0;right < n;right++){
    13. sum2 += nums[right];
    14. while(sum2 > target) sum2 -= nums[left++];
    15. if(sum2 == target) ret = max(ret,right - left + 1);
    16. }
    17. return ret == -1?-1:n - ret;
    18. }
    19. };

    5、水果成篮

     unordered_map版

    1. class Solution {
    2. public:
    3. int totalFruit(vector<int>& fruits) {
    4. unordered_map<int,int> hash;
    5. int n = fruits.size();
    6. int kind = 0,ret = 0;
    7. for(int left = 0,right= 0;right < n;right++){
    8. hash[fruits[right]]++;
    9. while(hash.size() > 2)
    10. {
    11. hash[fruits[left]]--;
    12. if(hash[fruits[left]] == 0) hash.erase(fruits[left]);
    13. left++;
    14. }
    15. ret = max(ret,right - left + 1);
    16. }
    17. return ret;
    18. }
    19. };

    数组版: 

    1. class Solution {
    2. public:
    3. int totalFruit(vector<int>& fruits) {
    4. int hash[100010] = {0};
    5. int n = fruits.size();
    6. int kind = 0,ret = 0;
    7. for(int left = 0,right= 0;right < n;right++){
    8. if(hash[fruits[right]] == 0) kind++;
    9. hash[fruits[right]]++;
    10. while(kind > 2)
    11. {
    12. hash[fruits[left]]--;
    13. if(hash[fruits[left]] == 0) kind--;
    14. left++;
    15. }
    16. ret = max(ret,right - left + 1);
    17. }
    18. return ret;
    19. }
    20. };

     6、找到字符串中所有字母异位词

    check的优化: 

    1. class Solution {
    2. public:
    3. vector<int> findAnagrams(string s, string p) {
    4. int hashp[265] = {0};
    5. for(auto x : p) hashp[x]++;
    6. int count = 0;
    7. vector<int> ret;
    8. int hashs[256] = {0};
    9. for(int l = 0,r = 0;r < s.size();r++){
    10. hashs[s[r]]++;
    11. if(hashs[s[r]] <= hashp[s[r]]) count++;
    12. if(r - l + 1 > p.size())
    13. {
    14. if(hashs[s[l]] <= hashp[s[l]]) count--;
    15. hashs[s[l]]--;
    16. l++;
    17. }
    18. if(count == p.size()) ret.push_back(l);
    19. }
    20. return ret;
    21. }
    22. };

    7、串联所有单词的子串(容器的使用)

     

    1. class Solution {
    2. public:
    3. vector<int> findSubstring(string s, vector& words) {
    4. unordered_mapint> hash1;
    5. vector<int> ret;
    6. for(auto& s:words){
    7. hash1[s]++;
    8. }
    9. int len = words[0].size(),m = words.size();
    10. for(int i = 0;i < len;i++){
    11. unordered_mapint> hash2;
    12. for(int left = i,right = i,count= 0; right + len <= s.size();right += len){
    13. //进窗口+维护count
    14. string in = s.substr(right,len);
    15. hash2[in]++;
    16. if(hash2[in] <= hash1[in]) count++;
    17. //判断
    18. if(right - left + 1 > len * m)
    19. {
    20. //出窗口+维护count
    21. string out = s.substr(left,len);
    22. if(hash2[out] <= hash1[out]) count--;
    23. hash2[out]--;
    24. left += len;
    25. }
    26. //更新结果
    27. if(count == m) ret.push_back(left);
    28. }
    29. }
    30. return ret;
    31. }
    32. };

    8、最小覆盖子串 

     

    1. class Solution {
    2. public:
    3. string minWindow(string s, string t) {
    4. int hash1[128] = {0};
    5. int kinds = 0;//统计有效字符种类数
    6. for(auto ch : t)
    7. {
    8. if(hash1[ch] == 0) kinds++;
    9. hash1[ch]++;
    10. }
    11. int hash2[128] = {0};
    12. int minlen = INT_MAX,begin = -1;
    13. for(int left = 0,right = 0,count = 0;right < s.size();right++){
    14. //进窗口
    15. char in = s[right];
    16. hash2[in]++;
    17. if(hash2[in] == hash1[in]) count++;
    18. while(count == kinds)
    19. {
    20. if(right - left+1 < minlen)
    21. {
    22. minlen = right - left + 1;
    23. begin = left;
    24. }
    25. char out = s[left++];
    26. if(hash2[out] == hash1[out]) count--;
    27. hash2[out]--;
    28. }
    29. }
    30. if(begin == -1) return "";
    31. else return s.substr(begin,minlen);
    32. }
    33. };

     

  • 相关阅读:
    Stable Diffusion扩散模型推导公式的基础知识
    VS中展开和折叠代码
    [Spring Boot 6]企业级开发
    Volcano:在离线作业混部管理平台,实现智能资源管理和作业调度
    公众号查题接口
    C# List
    XAF新手入门 - 类型子系统(Types Info Subsystem)
    QT中导出 Qt Tablewidget表格数据导出到 .csv文件
    如何用CSS实现10种现代布局
    微软Phi-3,3.8亿参数能与Mixtral 8x7B和GPT-3.5相媲美,量化后还可直接在IPhone中运行
  • 原文地址:https://blog.csdn.net/m0_73065213/article/details/133799930