Given an array of n positive integers and a positive integer s, find the minimal length of a subarray of which the sum ≥ s. If there isn't one, return 0 instead.
For example, given the array [2,3,1,2,4,3]
and s = 7
,
the subarray [4,3]
has the minimal length under the problem constraint.
代码如下:(超时)
public class Solution {
public int minSubArrayLen(int s, int[] nums) {
// int min=Integer.MAX_VALUE;
int min=nums.length+1; if(nums.length==0)
return 0; for(int i=0;i<nums.length-1;i++)
{
int sum=nums[i],count=1;
if(sum>=s)
return count;
for(int j=i+1;j<nums.length;j++)
{
sum+=nums[j];
count++;
if(count>=min)
break; if(sum>=s)
{
if(count<min)
min=count;
break;
}
} } if(min==nums.length+1)
return 0; return min;
}
}