faster implementation of sum ( for Codility test )

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

    100% correctness and performance of this code is tested

    Private Function equi(ByVal A() As Integer) As Integer
            Dim index As Integer = -1
            If A.Length > 0 And Not IsDBNull(A) Then
                Dim sumLeft As Long = 0
                Dim sumRight As Long = ArraySum(A)
                For i As Integer = 0 To A.Length - 1
                    Dim val As Integer = A(i)
    
                    sumRight -= val
                    If sumLeft = sumRight Then
                        index = i
                    End If
                    sumLeft += val
                Next
            End If
    
            Return index
        End Function
    

提交回复
热议问题