力扣209.长度最小的数组

力扣209.长度最小的数组

  • 模版滑窗求最小

cpp 复制代码
  class Solution {
  public:
      int minSubArrayLen(int target, vector<int>& nums) {
          int n = nums.size(),res=n+1;
          int sum = 0;
          for(int i=0,j=0;i<n;i++)
          {
              sum += nums[i];
              //尽可能缩小区间
              while(sum - nums[j] >= target)
              {
                  sum -= nums[j++];
              }
              if(sum >= target) res = min(res,i-j+1);
          }
          if(res == n+1) return 0;
          else return res;
      }
  };
相关推荐
LYFlied1 天前
【每日算法】LeetCode 136. 只出现一次的数字
前端·算法·leetcode·面试·职场和发展
唯唯qwe-1 天前
Day23:动态规划 | 爬楼梯,不同路径,拆分
算法·leetcode·动态规划
做科研的周师兄1 天前
中国土壤有机质数据集
人工智能·算法·机器学习·分类·数据挖掘
来深圳1 天前
leetcode 739. 每日温度
java·算法·leetcode
LYFlied1 天前
WebAssembly (Wasm) 跨端方案深度解析
前端·职场和发展·wasm·跨端
yaoh.wang1 天前
力扣(LeetCode) 104: 二叉树的最大深度 - 解法思路
python·程序人生·算法·leetcode·面试·职场和发展·跳槽
hetao17338371 天前
2025-12-21~22 hetao1733837的刷题笔记
c++·笔记·算法
醒过来摸鱼1 天前
递归三种分类方法
算法
炽烈小老头1 天前
【每天学习一点算法 2025/12/22】将有序数组转换为二叉搜索树
学习·算法
jghhh011 天前
POCS(凸集投影)算法解决部分k空间数据缺失导致吉布斯伪影
算法