一、题目
209. 长度最小的子数组 - 力扣(LeetCode)
二、代码
class Solution {
public:
int minSubArrayLen(int target, vector<int>& nums) {
int left = 0, right = 0;
int sum = nums[right];
int MinLength = INT_MAX;
while (left <nums.size() && right < nums.size())
{
if (sum < target)
{
if (right + 1 == nums.size())//因为后面有[++right]
{
break;
}
sum += nums[++right];
continue;
}
if (sum >= target)
{
MinLength = min(MinLength,right-left+1);
sum -= nums[left];
++left;
}
}
return MinLength== INT_MAX? 0 : MinLength;
}
};