【劍指Offer學習】【面試題31:連續子數組的最大和】

 

https://blog.csdn.net/wangaohan0728/article/details/88094581

 

bool maxSub(int a[],int len,int*ret) {
    if (a == NULL || 0 == len) {
        return false;
    }
    int total=0;
    int curTotal=0;
    for (int i = 0; i<len-1; i++) {
        if (curTotal + a[i] >=0) {
            curTotal = curTotal + a[i];
            if (curTotal >=total) {
                total = curTotal;
            }
        }
        else {
            curTotal = 0;
        }
    }
    *ret = total;
    return true;
}



    int maxsub[]  = {1, -2, 3, 10, -4, 7, 2, -5};
    int maxSubRet;
    maxSub(maxsub, 8, &maxSubRet);

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章