mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
Update 0209.长度最小的子数组.md
添加 0209.长度最小的子数组 Java版本
This commit is contained in:
@ -148,7 +148,24 @@ class Solution:
|
||||
|
||||
|
||||
Java:
|
||||
|
||||
```java
|
||||
class Solution {
|
||||
// 滑动窗口
|
||||
public int minSubArrayLen(int s, int[] nums) {
|
||||
int left = 0;
|
||||
int sum = 0;
|
||||
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 result == Integer.MAX_VALUE ? 0 : result;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Python:
|
||||
|
||||
|
Reference in New Issue
Block a user