-
Notifications
You must be signed in to change notification settings - Fork 1
/
leetcode209.java
58 lines (51 loc) · 1.26 KB
/
leetcode209.java
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
45
46
47
48
49
50
51
52
53
54
55
56
57
/**
* 题目:给一个数组nums和整数s,数组中全是正数,找出数组中的最短子集,使子集中数字之和>=s,如果不存在,返回0.
* 寻找的子集必须是连续的。
* @author ghb
*
* 思路:用两个指针,一个low,一个high, low和high之间的数字就是组成s的数组中的数字。
*/
public class Leetcode209 {
public static void main(String[] args){
int[] nums = {2,3,1,2,4,3};
int s = 7;
int result = minSubArrayLen(s, nums);
System.out.println(result);
}
public static int minSubArrayLen(int s, int[] nums) {
if(nums == null || nums.length == 0)
return 0;
int min = nums.length;
int low = 0; //low和high两个指针之间的数之和为count。
int high = 0;
int count = 0;
for(int i=0;i<nums.length;i++)
count += nums[i];
if(count<s)
return 0;
count = 0;
for(high=0;high<nums.length;high++){
count += nums[high];
if(count>=s)
break;
}
min = high-low+1; //设置min的初值
while(low<=high){
while(count>=s){
count -= nums[low];
low++;
}
if(count<s){
low--;
count += nums[low];
if(high-low+1<min)
min = high-low+1;
if(high==nums.length-1)
break;
}
high++;
count += nums[high];
}
return min;
}
}