Merge pull request #83 from Joshua-Lu/patch-1

更新 0209.长度最小的子数组 Java版本
This commit is contained in:
Carl Sun
2021-05-14 09:37:14 +08:00
committed by GitHub

View File

@ -150,19 +150,20 @@ class Solution:
Java
```java
class Solution {
public int minSubArrayLen(int target, int[] nums) {
Integer size = Integer.MAX_VALUE;
int from = 0;
int to = 0;
// 滑动窗口
public int minSubArrayLen(int s, int[] nums) {
int left = 0;
int sum = 0;
while (to < nums.length) {
sum += nums[to++];
while (sum >= target) {
size = Math.min(size,to - from);
sum -= nums[from++];
int result = Integer.MAX_VALUE;
for (int right = 0; right < nums.length; right++) {
sum += nums[right];
while (sum >= s) {
result = Math.min(result, right - left + 1);
sum -= nums[left++];
}
}
return size == Integer.MAX_VALUE ? 0 : size;
return result == Integer.MAX_VALUE ? 0 : result;
}
}
```