Codility 解决方案 [Swift 3.0]

[Codility] Lession 4.3 MissingIn

2016-07-27  本文已影响71人  sunlitamo

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
}
上一篇下一篇

猜你喜欢

热点阅读