LC209双指针


 1 #include "stdio.h"
 2 int main()
 3 {
 4     int target = 7;
 5     int nums[6] = {2,3,1,2,4,3};
 6     int numsSize = 6;
 7     int point1,point2;
 8     long long int sum = 0;
 9     int min = 999999;
10     for(point1 = 0,point2 = 0;point1 < numsSize;point1++)
11     {
12         sum += nums[point1];
13         while(sum - nums[point2] >= target)
14             sum -= nums[point2++];
15         if(sum >= target)
16             min = min > point1 - point2 + 1 ? point1 - point2 + 1 : min;
17     }
18     if(sum >= target)
19         printf("%d",min);
20     else 
21         printf("0");
22 }