-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathP_209.java
More file actions
31 lines (28 loc) · 872 Bytes
/
Copy pathP_209.java
File metadata and controls
31 lines (28 loc) · 872 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
package leetcode.medium;
public class P_209 {
public int minSubArrayLen(int s, int[] nums) {
int l = 0, r = 0, sum = 0, res = Integer.MAX_VALUE;
while (r < nums.length) {
sum += nums[r++];
while (sum >= s) {
res = Math.min(res, r - l);
sum -= nums[l++];
}
}
return res == Integer.MAX_VALUE ? 0 : res;
}
public int minSubArrayLen2(int s, int[] nums) {
int res = Integer.MAX_VALUE;
for (int i = 0; i < nums.length; i++) {
int sum = 0;
for (int j = i; j < nums.length; j++) {
sum += nums[j];
if (sum >= s) {
res = Math.min(res, j - i + 1);
break;
}
}
}
return res == Integer.MAX_VALUE ? 0 : res;
}
}