leetcode-209.长度最小的子数组
文章目录
- leetcode-209.长度最小的子数组
- 题目描述
- 代码提交(快慢指针-滑动窗口)
题目描述
代码提交(快慢指针-滑动窗口)
代码
class Solution {
public:
int minSubArrayLen(int target, vector<int> &nums) {
int slow = 0;
int fast = 0;
int sum = 0;
int length_min = INT32_MAX;
while (fast < nums.size()) {
sum = sum + nums[fast];
while (sum >= target) {
sum = sum - nums[slow];
length_min = (length_min > fast - slow + 1) ? fast - slow + 1 : length_min;
slow++;
}
fast++;
}
return (length_min == INT32_MAX) ? 0 : length_min;
}
};