Minimum Size Subarray Sum
给一个数组和一个整数s, 找最小长度的子数组和大于s. 这个用两个指针, 前后往中间扫.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
public class Solution { public int minSubArrayLen(int s, int[] nums) { int len = Integer.MAX_VALUE; int i = 0; int j = 0; int sum = 0; while(i < nums.length) { sum += nums[i]; while(sum >= s && j < nums.length){ sum -= nums[j]; len = Math.min(len, i - j + 1); j++; } i++; } if(len == Integer.MAX_VALUE) return 0; else return len; } } |