209.Minimum Size Subarray Sum

Solution 1: accepted 3ms

Two pointers.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Solution {
public int minSubArrayLen(int s, int[] nums) {
int start = 0;
int end = 0;
int sum = 0;
int min = Integer.MAX_VALUE;
for (int i = 0; i < nums.length; i++) {
sum += nums[i];
end = i;
if (sum >= s) {
while (sum - nums[start] >= s) {
sum -= nums[start];
start++;
}
min = Math.min(min, end - start + 1);
}
}
return min == Integer.MAX_VALUE ? 0 : min;
}
}