代码随想录:长度最小的子数组
现在不像考研那时候,每天时间都是固定的,以后可能还是以周为单位定目标比较好一点
滑动窗口问题,之后记得和计算机网络里的滑动窗口对比,并且和背包问题对比
class Solution {
public:
int minSubArrayLen(int target, vector<int>& nums) {
int sum = 0;
int min1 = target + 1;
int left = 0;
int right = 0;
while (right < nums.size()) {
sum = sum + nums[right];
while (sum >= target) {
min1 = min(min1, right - left + 1);
sum = sum - nums[left];
left++;
}
right++;
}
if (min1 == target + 1) {
return 0;
} else {
return min1;
}
}
};
标签:right,target,nums,int,sum,随想录,数组,min1,长度
From: https://www.cnblogs.com/huigugu/p/18550579