Get balance noe

a[0],a[1],a[2],a[1],a[1]
1      2     3     4      5   
balance noe is 3

C:

int BalanceNode(int *a, int length)
{
    int *begin=a, *end=a+length;
    int leftsum=0, rightsum=0;
    int position=0;

    while(position<length)
    {
        rightsum+=a[position];
        position++;
    }

    //begin compute
    position=0;
    while(begin<end)
    {
        if(leftsum==rightsum-a[position])
        return position;
        leftsum+=a[position];
        rightsum-=a[position];
        begin++;
        position++;
    }
    return -1;
}

posted @ 2007-09-24 21:31  HonestMan  阅读(163)  评论(0编辑  收藏  举报