LC 209 Minimum Size Subarray Sum(M)
解题思路 DP
public int minSubArrayLen(int s, int[] nums) {
if(nums.length<1)
return 0;
Queue<Integer> q = new LinkedList<Integer>();
int minLen = Integer.MAX_VALUE;
int total= 0;
for(int i = 0; i < nums.length; i++){
total += nums[i];
q.offer(nums[i]);
if(total >= s){
while(!q.isEmpty() && total>=s){
total -= q.poll();
}
minLen = Math.min(minLen, q.size() + 1);
}
}
return minLen == Integer.MAX_VALUE? 0 : minLen;
}Last updated