Minimum Size Subarray Sum
class Solution {
public int minSubArrayLen(int s, int[] nums) {
if(nums == null || nums.length == 0)
return 0;
int cnt = Integer.MAX_VALUE, n = nums.length, sum = 0;
for(int i = 0, j = 0; i < n; i ++){
//直接while开始,不需要i,j开始错开。
while(j < n && sum < s){
sum += nums[j ++];
}
//while里俩条件,不保证sum存在,所以此处需要再判断。
if(s <= sum){
cnt = Math.min(j - i, cnt);
}
sum -= nums[i];
}
if(cnt == Integer.MAX_VALUE){
return 0;
}
return cnt;
}
}Last updated