faster implementation of sum ( for Codility test )

前端 未结 22 2093
鱼传尺愫
鱼传尺愫 2021-02-04 11:47

How can the following simple implementation of sum be faster?

private long sum( int [] a, int begin, int end ) {
    if( a == null   ) {
        ret         


        
22条回答
  •  走了就别回头了
    2021-02-04 12:22

    private static int equi ( int[] A ) {
        if (A == null || A.length == 0)
         return -1;
     long tot = 0;
     int len = A.length;
     for(int i=0;i

    }

    I considered the array as a bilance so if the equilibrium index exist then half of the weight is on the left. So I only compare the partTot (partial total) x 2 with the total weight of the array. the Alg takes O(n) + O(n)

提交回复
热议问题