class Solution {
public:
int minSubArrayLen(int target, vector<int>& nums) {
int start = 0;
int end = 0;
int minLength = nums.size() + 1;
int curSum = 0;
while(end < nums.size()){
curSum += nums[end];
while(curSum >= target){ //滑动窗口,维护窗口内元素总和
minLength = min(minLength, end - start + 1);
curSum -= nums[start++];
}
end ++;
}
if(minLength == nums.size() + 1) return 0;
else return minLength;
}
};
<Talk is cheap, show me the code 32> LeetCode.209 长度最小的子数组
最新推荐文章于 2024-11-16 21:36:47 发布