The problem is basically find the index which make the sum of the numbers before the index and the sum of the numbers after the index equal. When I try to solve this problem on leetcode,I always get a heap buffer overflow warning on the
aSum = aSum + *(nums+2*x);
The full code:
int findMiddleIndex(int* nums, int numsSize) {
for(int i=1; i<=numsSize; i++){
int bSum = 0;
int aSum = 0;
//calculate the sum of the numbers before the index and after the index
for(int j=1; j<i; j++){
bSum = bSum + *(nums+2*j);
}
for(int x=i+1; x<=numsSize; x++){
aSum = aSum + *(nums+2*x);
}
//check these two sums and return
if(aSum == bSum){
if(i==1){
return 0;
}
if(i==numsSize){
return numsSize-1;
}
return i-1;
}
}
return -1;
}
I try to know if there is a reason on the aspect of the syntax in
*(nums+2*x)
and even change the number infront of “x”,but it seems to be another reason.