https://leetcode.com/problems/minimum-size-subarray-sum/#/description
找到子數組的和大於等於s的最短子數組
非常簡單,找到滿足條件的邊界條件,不斷update
public class Solution {
public int minSubArrayLen(int s, int[] nums) {
if (nums == null || nums.length == 0) {
return 0;
}
int beg = 0;
int end = 0;
int sum = 0;
int len = Integer.MAX_VALUE;
while (end < nums.length) {
sum += nums[end++];
while (sum >= s) {
len = Math.min(len, end - beg);
sum -= nums[beg++];
}
}
return len == Integer.MAX_VALUE ? 0 : len;
}
}