Skip to content

Commit 947ce9d

Browse files
committed
209-minimum-size-subarray-sum.md Separated Chinese solutions from English solutions.
1 parent b867080 commit 947ce9d

File tree

2 files changed

+38
-161
lines changed

2 files changed

+38
-161
lines changed

en/1-1000/209-minimum-size-subarray-sum.md

Lines changed: 8 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
# 209. Minimum Size Subarray Sum - Best Practices of LeetCode Solutions
2-
LeetCode link: [209. Minimum Size Subarray Sum](https://leetcode.com/problems/minimum-size-subarray-sum),
3-
[209. 长度最小的子数组](https://leetcode.cn/problems/minimum-size-subarray-sum)
2+
LeetCode link: [209. Minimum Size Subarray Sum](https://leetcode.com/problems/minimum-size-subarray-sum), difficulty: **Medium**.
43

5-
[中文题解](#中文题解)
6-
7-
## LeetCode problem description
4+
## LeetCode description of "209. Minimum Size Subarray Sum"
85
Given an array of positive integers `nums` and a positive integer `target`, return _the **minimal length** of a **subarray** whose sum is greater than or equal to `target`_. If there is no such subarray, return `0` instead.
96

10-
* A **subarray** is a contiguous non-empty sequence of elements within an array.
11-
12-
Difficulty: **Medium**
7+
> * A **subarray** is a contiguous non-empty sequence of elements within an array.
138
149
### [Example 1]
1510
**Input**: `target = 7, nums = [2,3,1,2,4,3]`
@@ -29,16 +24,16 @@ Difficulty: **Medium**
2924
**Output**: `0`
3025

3126
### [Constraints]
32-
- `1 <= target <= 10 ** 9`
33-
- `1 <= nums.length <= 100000`
27+
- `1 <= target <= 10^9`
28+
- `1 <= nums.length <= 10^5`
3429
- `1 <= nums[i] <= 10000`
3530

3631
## Intuition
3732
For **subarray** problems, you can consider using **Sliding Window Technique**, which is similar to the **Fast and Slow Pointers Technique**.
3833

3934
## Steps
40-
* Iterate over the `nums` array, the `index` of the element is named `fastIndex`. Although inconspicuous, this is the most important logic of the _Fast and Slow Pointers Technique_. Please memorize it.
41-
* `sum += nums[fast_index]`.
35+
1. Iterate over the `nums` array, the `index` of the element is named `fastIndex`. Although inconspicuous, this is the most important logic of the _Fast and Slow Pointers Technique_. Please memorize it.
36+
2. `sum += nums[fast_index]`.
4237
```java
4338
var minLength = Integer.MAX_VALUE;
4439
var sum = 0;
@@ -51,7 +46,7 @@ for (var fastIndex = 0; fastIndex < nums.length; fastIndex++) { // This line the
5146
return minLength;
5247
```
5348

54-
* Control of `slowIndex`:
49+
3. Control of `slowIndex`:
5550
```java
5651
var minLength = Integer.MAX_VALUE;
5752
var sum = 0;
@@ -221,61 +216,3 @@ public class Solution
221216
```
222217
// Welcome to create a PR to complete the code of this language, thanks!
223218
```
224-
225-
## 力扣问题描述
226-
给定一个含有 `n` 个正整数的数组和一个正整数 `target`
227-
228-
找出该数组中满足其总和大于等于 `target` 的长度**最小的** **子数组** `[numsl, numsl+1, ..., numsr-1, numsr]` ,并返回*其长度*。如果不存在符合条件的子数组,返回 `0`
229-
230-
**子数组** 是数组中连续的 **非空** 元素序列。
231-
232-
难度: **中等**
233-
234-
### [示例 1]
235-
**输入**: `target = 7, nums = [2,3,1,2,4,3]`
236-
237-
**输出**: `2`
238-
239-
**解释**: `子数组 [4,3] 是该条件下的长度最小的子数组。`
240-
241-
# 中文题解
242-
## 思路
243-
1. 对于**子数组**问题,可以考虑使用**滑动窗口技术**,它类似于**快速和慢速指针技术**
244-
245-
## 步骤
246-
1. **遍历** `nums` 数组,元素的 `index` 可命名为 `fastIndex`。虽然不起眼,但这是 `快慢指针技术` **最重要**的逻辑。请最好记住它。
247-
2. `sum += nums[fast_index]`.
248-
```java
249-
var minLength = Integer.MAX_VALUE;
250-
var sum = 0;
251-
var slowIndex = 0;
252-
253-
for (var fastIndex = 0; fastIndex < nums.length; fastIndex++) { // 本行是`快慢指针技术`最重要的逻辑
254-
sum += nums[fastIndex]; // 1
255-
}
256-
257-
return minLength;
258-
```
259-
260-
3. 控制`slowIndex`
261-
```java
262-
var minLength = Integer.MAX_VALUE;
263-
var sum = 0;
264-
var slowIndex = 0;
265-
266-
for (var fastIndex = 0; fastIndex < nums.length; fastIndex++) {
267-
sum += nums[fastIndex];
268-
269-
while (sum >= target) { // 1
270-
minLength = Math.min(minLength, fastIndex - slowIndex + 1); // 2
271-
sum -= nums[slowIndex]; // 3
272-
slowIndex++; // 4
273-
}
274-
}
275-
276-
if (minLength == Integer.MAX_VALUE) { // 5
277-
return 0; // 6
278-
}
279-
280-
return minLength;
281-
```

zh/1-1000/209-minimum-size-subarray-sum.md

Lines changed: 30 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,55 @@
1-
# 209. Minimum Size Subarray Sum - Best Practices of LeetCode Solutions
2-
LeetCode link: [209. Minimum Size Subarray Sum](https://leetcode.com/problems/minimum-size-subarray-sum),
3-
[209. 长度最小的子数组](https://leetcode.cn/problems/minimum-size-subarray-sum)
1+
# 209. 长度最小的子数组 - 力扣题解最佳实践
2+
力扣链接:[209. 长度最小的子数组](https://leetcode.cn/problems/minimum-size-subarray-sum) ,难度:**中等**
43

5-
[中文题解](#中文题解)
6-
7-
## LeetCode problem description
8-
Given an array of positive integers `nums` and a positive integer `target`, return _the **minimal length** of a **subarray** whose sum is greater than or equal to `target`_. If there is no such subarray, return `0` instead.
4+
## 力扣“209. 长度最小的子数组”问题描述
5+
给定一个含有 `n` 个正整数的数组和一个正整数 `target`
96

10-
* A **subarray** is a contiguous non-empty sequence of elements within an array.
7+
找出该数组中满足其总和大于等于 `target` 的长度**最小的** **子数组** `[numsl, numsl+1, ..., numsr-1, numsr]` ,并返回*其长度*。如果不存在符合条件的子数组,返回 `0`
118

12-
Difficulty: **Medium**
9+
> **子数组** 是数组中连续的 **非空** 元素序列。
1310
14-
### [Example 1]
15-
**Input**: `target = 7, nums = [2,3,1,2,4,3]`
11+
### [示例 1]
12+
**输入**: `target = 7, nums = [2,3,1,2,4,3]`
1613

17-
**Output**: `2`
14+
**输出**: `2`
1815

19-
**Explanation**: `The subarray [4,3] has the minimal length under the problem constraint.`
16+
**解释**: `子数组 _[4,3]_ 是该条件下的长度最小的子数组。`
2017

21-
### [Example 2]
22-
**Input**: `target = 4, nums = [1,4,4]`
18+
### [示例 2]
19+
**输入**: `target = 4, nums = [1,4,4]`
2320

24-
**Output**: `1`
21+
**输出**: `1`
2522

26-
### [Example 3]
27-
**Input**: `target = 11, nums = [1,1,1,1,1,1,1,1]`
23+
### [示例 3]
24+
**输入**: `target = 11, nums = [1,1,1,1,1,1,1,1]`
2825

29-
**Output**: `0`
26+
**输出**: `0`
3027

31-
### [Constraints]
32-
- `1 <= target <= 10 ** 9`
33-
- `1 <= nums.length <= 100000`
28+
### [约束]
29+
- `1 <= target <= 10^9`
30+
- `1 <= nums.length <= 10^5`
3431
- `1 <= nums[i] <= 10000`
3532

36-
## Intuition
37-
For **subarray** problems, you can consider using **Sliding Window Technique**, which is similar to the **Fast and Slow Pointers Technique**.
33+
## 思路
34+
1. 对于**子数组**问题,可以考虑使用**滑动窗口技术**,它类似于**快速和慢速指针技术**
35+
3836

39-
## Steps
40-
* Iterate over the `nums` array, the `index` of the element is named `fastIndex`. Although inconspicuous, this is the most important logic of the _Fast and Slow Pointers Technique_. Please memorize it.
41-
* `sum += nums[fast_index]`.
37+
## 步骤
38+
1. **遍历** `nums` 数组,元素的 `index` 可命名为 `fastIndex`。虽然不起眼,但这是 `快慢指针技术` **最重要**的逻辑。请最好记住它。
39+
2. `sum += nums[fast_index]`.
4240
```java
4341
var minLength = Integer.MAX_VALUE;
4442
var sum = 0;
4543
var slowIndex = 0;
4644

47-
for (var fastIndex = 0; fastIndex < nums.length; fastIndex++) { // This line the most important logic of the `Fast and Slow Pointers Technique`.
45+
for (var fastIndex = 0; fastIndex < nums.length; fastIndex++) { // 本行是`快慢指针技术`最重要的逻辑
4846
sum += nums[fastIndex]; // 1
4947
}
5048

5149
return minLength;
5250
```
5351

54-
* Control of `slowIndex`:
52+
3. 控制`slowIndex`
5553
```java
5654
var minLength = Integer.MAX_VALUE;
5755
var sum = 0;
@@ -74,9 +72,9 @@ if (minLength == Integer.MAX_VALUE) { // 5
7472
return minLength;
7573
```
7674

77-
## Complexity
78-
* Time: `O(n)`.
79-
* Space: `O(1)`.
75+
## 复杂度
76+
* 时间: `O(n)`.
77+
* 空间: `O(1)`.
8078

8179
## Java
8280
```java
@@ -221,61 +219,3 @@ public class Solution
221219
```
222220
// Welcome to create a PR to complete the code of this language, thanks!
223221
```
224-
225-
## 力扣问题描述
226-
给定一个含有 `n` 个正整数的数组和一个正整数 `target`
227-
228-
找出该数组中满足其总和大于等于 `target` 的长度**最小的** **子数组** `[numsl, numsl+1, ..., numsr-1, numsr]` ,并返回*其长度*。如果不存在符合条件的子数组,返回 `0`
229-
230-
**子数组** 是数组中连续的 **非空** 元素序列。
231-
232-
难度: **中等**
233-
234-
### [示例 1]
235-
**输入**: `target = 7, nums = [2,3,1,2,4,3]`
236-
237-
**输出**: `2`
238-
239-
**解释**: `子数组 [4,3] 是该条件下的长度最小的子数组。`
240-
241-
# 中文题解
242-
## 思路
243-
1. 对于**子数组**问题,可以考虑使用**滑动窗口技术**,它类似于**快速和慢速指针技术**
244-
245-
## 步骤
246-
1. **遍历** `nums` 数组,元素的 `index` 可命名为 `fastIndex`。虽然不起眼,但这是 `快慢指针技术` **最重要**的逻辑。请最好记住它。
247-
2. `sum += nums[fast_index]`.
248-
```java
249-
var minLength = Integer.MAX_VALUE;
250-
var sum = 0;
251-
var slowIndex = 0;
252-
253-
for (var fastIndex = 0; fastIndex < nums.length; fastIndex++) { // 本行是`快慢指针技术`最重要的逻辑
254-
sum += nums[fastIndex]; // 1
255-
}
256-
257-
return minLength;
258-
```
259-
260-
3. 控制`slowIndex`
261-
```java
262-
var minLength = Integer.MAX_VALUE;
263-
var sum = 0;
264-
var slowIndex = 0;
265-
266-
for (var fastIndex = 0; fastIndex < nums.length; fastIndex++) {
267-
sum += nums[fastIndex];
268-
269-
while (sum >= target) { // 1
270-
minLength = Math.min(minLength, fastIndex - slowIndex + 1); // 2
271-
sum -= nums[slowIndex]; // 3
272-
slowIndex++; // 4
273-
}
274-
}
275-
276-
if (minLength == Integer.MAX_VALUE) { // 5
277-
return 0; // 6
278-
}
279-
280-
return minLength;
281-
```

0 commit comments

Comments
 (0)