[Codility] Lession 3.1 TapeEquilibrium

Swift Solution:

public func solution(inout A : [Int]) -> Int {
    guard A.count > 0 else { return 0 }
    
    var leftSum = A[0]
    var rightSum = 0
    
    var diff = [Int]()

    for i in 1 ..< A.count { rightSum += A[i] }

    for j in 1 ..< A.count {
        let temp = abs(leftSum - rightSum)
        
        leftSum += A[j]
        rightSum -= A[j]
        
        diff.append(temp)
    }
    var result = diff[0]
    for x in 0 ..< diff.count {
        if diff[x] < result {  result = diff[x] }
    }
    return result

你可能感兴趣的:([Codility] Lession 3.1 TapeEquilibrium)