滑动窗口, 当windowSum >= target的时候记录答案, 然后滑动left到 windowSum < target

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Solution {
/**
* 滑动窗口, 当windowSum >= target的时候记录答案,
* 然后滑动left到 windowSum < target
*/
public int minSubArrayLen(int target, int[] nums) {
int ansLeft = -1, ansRight = Integer.MAX_VALUE / 2;
int left = 0;
for (int right = 0; right < nums.length; right++) {
target -= nums[right];
if (target <= 0) {
while (target <= 0) {
target += nums[left++];
}
if (ansRight - ansLeft > right - left + 1) {
ansRight = right;
ansLeft = left - 1;
}
}
}
return ansRight == Integer.MAX_VALUE / 2
? 0
: ansRight - ansLeft + 1;
}
}

Comments