-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathSolution209.java
More file actions
38 lines (33 loc) · 818 Bytes
/
Solution209.java
File metadata and controls
38 lines (33 loc) · 818 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package algorithm.leetcode;
/**
* @author: mayuan
* @desc: 长度最小的子数组
* @date: 2019/01/04
*/
public class Solution209 {
public int minSubArrayLen(int s, int[] nums) {
if (0 > s || null == nums || 1 > nums.length) {
return 0;
}
int i = 0, j = -1;
int sum = 0;
int len = nums.length + 1;
while (i < nums.length) {
if (j < nums.length - 1 && sum < s) {
++j;
sum += nums[j];
} else {
sum -= nums[i];
++i;
}
if (sum >= s) {
len = len < j - i + 1 ? len : j - i + 1;
}
}
if (len == nums.length + 1) {
return 0;
} else {
return len;
}
}
}