Given an array of numbersnums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once.
For example:
Givennums = [1, 2, 1, 3, 2, 5], return[3, 5].
Note:
The order of the result is not important. So in the above example,[5, 3]is also correct.
Your algorithm should run in linear runtime complexity. Could you implement it using only constant space complexity?
數組中有一個單獨的數比較簡單, 異或起來就好了, 可是這個題有兩個只出現一次的數字。 那么異或起來就就是兩個數的異或, 那怎么再把這兩個數剝離開呢。?
先取得這個數最后一個1的數,也就是第一個不一樣位值的數字, 比如001^101 == 100, 最后一位不相等的數字就是第一位的1. 怎么求出 A &~(A-1) =100
然后用這個數再逐個與上數組中的每個值, 根據與的值1 / 0 剝離開。