寻找平衡点有关问题

寻找平衡点问题

平衡点:比如int numbers[]={1,3,5,7,8,25,4,20}; 25前面的总和为24,25后面的总和也是24,25这个点就是平衡点。假如一个数组中的元素,其前面的部分等于后面的部分,那么这个点的位序就是平衡点,要求返回任何一个平衡点。

寻找平衡点有关问题
int calcBalance(int arr[], int length)
{
    int *left = new int[length]; //left[i]为从第0个到第i-1个的和
    int *right = new int[length]; //right[i]为从第i+1个到第len-1个的和
    int b = length-1;
    for(int i=0; i<length; i++)
    {
    if(i == 0)   
            left[i] = 0;   
        else  
            left[i] = left[i-1]+arr[i-1];   
    }   
    for(; b>=0; b--)   
    {   
        if(b == length-1)   
            right[b] = 0;    
        else  
            right[b] = right[b+1]+arr[b+1];   
        if(left[b] == right[b])
        {
            delete[] left;
            delete[] right;
            return b;
        }
    }

    delete[] left;
    delete[] right;
    return -1;
}