faster implementation of sum ( for Codility test )

前端 未结 22 2081
鱼传尺愫
鱼传尺愫 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:44

    Here is my solution and I scored 100%

     public static int solution(int[] A)
        {
            double sum = A.Sum(d => (double)d);
            double leftSum=0;
            for (int i = 0; i < A.Length; i++){
                if (leftSum == (sum-leftSum-A[i])) {
                    return i;
                }
                else {
                    leftSum = leftSum + A[i];
                }
            }
            return -1;
        }
    

提交回复
热议问题