singleNumberIII swift


  • 0
    R

    class Solution {
    static func singleNumber(nums: [Int]) -> [Int] {

        //1,找到两个数的异或
        var diff = 0
        for num in nums {
            diff ^= num
        }
        //2,设置得到最后的bit
        diff &= -diff
        
        //3,返回数组的2个号码
        var ans : [Int] = [0,0]
    
        for num in nums {
            //bit 没有设置
            if (num & diff) == 0 {
                ans[0] ^= num
            } else {
                //该bit设置
                ans[1] ^= num
            }
        }
        
        return ans
    }
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.