[Codility] Lession 4.3 MissingInteger

Swift Solution:

import Foundation

public func solution(inout A : [Int]) -> Int {
    
    guard A.count > 0 else { return 0 }
    
    var result = 0
    
    var arr = [Bool](count: A.count + 1, repeatedValue: false)
    
    for i in 0 ..< A.count {
        if A[i] > 0 && A[i] <= A.count {
            arr[A[i] - 1] = true
        }
    }
    
    for idx in 0 ..< arr.count {
        if (arr[idx]) == false{
            result = idx + 1
            break
        }
    }
    return result
}

你可能感兴趣的:([Codility] Lession 4.3 MissingInteger)