leetcode1013(判斷數組是否能夠三等分)--C語言實現

求:數組

給你一個整數數組 A,只有能夠將其劃分爲三個和相等的非空部分時才返回 true,不然返回 falsespa

形式上,若是能夠找出索引 i+1 < j 且知足 (A[0] + A[1] + ... + A[i] == A[i+1] + A[i+2] + ... + A[j-1] == A[j] + A[j-1] + ... + A[A.length - 1]) 就能夠將數組三等分。code

示例 1:索引

輸出:[0,2,1,-6,6,-7,9,1,2,0,1]
輸出:true
解釋:0 + 2 + 1 = -6 + 6 - 7 + 9 + 1 = 2 + 0 + 1

示例 2:class

輸入:[0,2,1,-6,6,7,9,-1,2,0,1]
輸出:false

示例 3:di

輸入:[3,3,6,5,-2,2,5,1,-9,4]
輸出:true
解釋:3 + 3 = 6 = 5 - 2 + 2 + 5 + 1 - 9 + 4

解:co

bool  canThreePartsEqualSum( int * A,  int  ASize){
     int  i;
     int  sum= 0 ;
     for (i= 0 ;i<ASize;i++){
        sum+=A[i];
    }
     if (sum% 3 == 0 ){
         int  j,k,s1,s2;
        s1=s2= 0 ;
         for (j= 0 ;j<ASize;j++){
            s1+=A[j];
             if (s1==sum/ 3 ){
                 for (k=j+ 1 ;k<ASize- 1 ;k++){
                    s2+=A[k];
                     if (s2==sum/ 3 ){
                         return   true ;
                    }
                }
            }
        }
    }
     return   false ;
}
相關文章
相關標籤/搜索