LeetCode209:
给定一个含有 n 个正整数的数组和一个正整数 target 。
找出该数组中满足其和 ≥ target 的长度最小的 连续子数组 [numsl, numsl+1, …, numsr-1, numsr] ,并返回其长度。如果不存在符合条件的子数组,返回 0 。
class Solution {public int minSubArrayLen(int target, int[] nums) {int len = nums.length;LinkedList<Integer> list = new LinkedList<>();int sum = 0;int minLen = len;boolean flag = false;for (int i = 0; i < len; i++) {while (!list.isEmpty() && sum >= target) {flag = true;minLen = Math.min(minLen, list.size());int num = list.pollFirst();sum -= num;}sum += nums[i];list.addLast(nums[i]);}while (!list.isEmpty() && sum >= target) {flag = true;minLen = Math.min(minLen, list.size());int num = list.pollFirst();sum -= num;}return flag ? minLen : 0;}}
