mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
@ -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;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
Reference in New Issue
Block a user