[Codility] Lession 3.3 PermMissingElem

Swift Solution 1:

public func solution(inout A : [Int]) -> Int {
    var m = 1
    var n = 0
    
    for a in A {
        n = n ^ a ^ m
        m += 1
    }

    n = n ^ m
    return n
}

Swift Solution 2:

public func solution(inout A : [Int]) -> Int {
 
    var result = 0
    
    var xOr1:Int = 0
    var xOr2:Int = 0
    
    var idealArr = [Int]()

    for i in 1 ... A.count + 1 { idealArr.append(i) }
    
    for i in 0 ..< idealArr.count { xOr1 ^= idealArr[i] }

    for i in 0 ..< A.count { xOr2 ^= A[i] }
    
    result = xOr1^xOr2
    
    return result
}

Swift Solution 3:

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

你可能感兴趣的:([Codility] Lession 3.3 PermMissingElem)