z*********8 发帖数: 2070 | 1 Give an array of positive integers and a target positive integer, find the
continuous subarray whose sum is closest to the target.
谁能给个简洁清晰的代码? | p*****2 发帖数: 21240 | | g*********e 发帖数: 14401 | 3 void closestSum(int arr[], int n, int t){
int prev=0;
int post=0;
int curSum=arr[0];
int resPrev, resPost;
int closeSum=arr[0];
while(1){
if(curSum==t){
resPrev=prev;
resPost=post;
break;
}
else if(curSum
if(prev==n-1)
break;
else{
prev++;
curSum+=arr[prev];
}
}
else{
curSum-=arr[post++];
}
if(abs(curSum-t)
closeSum=curSum;
}
cout<<"closest sum="<
} |
|