privateintwhileLoopVersionSlidingWindow(int target, int[] nums) { intleft=0, right = 0; intsum= nums[0]; intres= Integer.MAX_VALUE; while (left <= right) { if (sum < target) { right++; if (right < nums.length) { sum += nums[right]; } elsebreak; } else { res = Math.min(res, right - left + 1); if (left + 1 < nums.length) { sum -= nums[left]; left++; } elsebreak; } } return res == Integer.MAX_VALUE ? 0 : res; }
privateintforLoopVersionSlidingWindow(int[] nums, int s) { intleft=0; intsum=0; intresult= Integer.MAX_VALUE; for (intright=0; right < nums.length; right++) { sum += nums[right]; while (sum >= s) { result = Math.min(result, right - left + 1); sum -= nums[left++]; } } return result == Integer.MAX_VALUE ? 0 : result; }