美文网首页Codility 解决方案 [Swift 3.0]
[Codility] Lession 3.3 PermMissi

[Codility] Lession 3.3 PermMissi

作者: sunlitamo | 来源:发表于2016-07-22 13:30 被阅读72次

    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 PermMissi

        本文链接:https://www.haomeiwen.com/subject/bejijttx.html