Uh oh!
There was an error while loading. Please reload this page.
forked from liuyubobobo/Play-with-Algorithm-Interview
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution4.java
More file actions
Latest commit
44 lines (34 loc) · 1.19 KB
/
Copy pathSolution4.java
File metadata and controls
44 lines (34 loc) · 1.19 KB
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
39
40
41
42
43
44
// 209. Minimum Size Subarray Sum
// https://leetcode.com/problems/minimum-size-subarray-sum/description/
//
// 另外一个滑动窗口的实现, 仅供参考
// 时间复杂度: O(n)
// 空间复杂度: O(1)
publicclassSolution4 {
publicintminSubArrayLen(ints, int[] nums) {
if(s <= 0 || nums == null)
thrownewIllegalArgumentException("Illigal Arguments");
intl = 0 , r = -1; // [l...r]为我们的窗口
intsum = 0;
intres = nums.length + 1;
while(r + 1 < nums.length){ // 窗口的右边界无法继续扩展了, 则循环继续
while(r + 1 < nums.length && sum < s)
sum += nums[++r];
if(sum >= s)
res = Math.min(res, r - l + 1);
while(l < nums.length && sum >= s){
sum -= nums[l++];
if(sum >= s)
res = Math.min(res, r - l + 1);
}
}
if(res == nums.length + 1)
return0;
returnres;
}
publicstaticvoidmain(String[] args) {
int[] nums = {2, 3, 1, 2, 4, 3};
ints = 7;
System.out.println((newSolution4()).minSubArrayLen(s, nums));
}
}