This is a creation in Article, where the information may have evolved or changed.
Given An array of numbers nums , in which exactly-elements appear only once and all the other elements appear exactly Twice. Find the elements that appear only once.
For example:
Given nums = [1, 2, 1, 3, 2, 5] , return [3, 5] .
Note:
- The order of the result is not important. The above example, is
[5, 3] also correct.
- Your algorithm should run in linear runtime complexity. Could implement it using only constant space complexity?
The main idea of the topic is: Given an array, the number of the array in addition to two different numbers, the remaining number 20% pairs appear, now to find out the two numbers ---------------------------------------------------------------------------------------------- ------------------------------------------------------------------------------ This problem is not easy to take in hand, referring to the results of others "http://www.jianshu.com/p/402622b8ad43" briefly say the idea of solving problems, first of all the number of different or operation, then the same will be offset, the result is two different number A and B of the XOR, this result as a diff then Ask (-diff & diff) This equation can get only a 1 of the number in the binary, the position of this 1 represents in this position, A and B are different, then you can follow this difference, divide all the numbers into two groups, the first set is the same as a at that location, and the second group is the same as b at that location.
Func Singlenumber (nums []int) []int { diff: = 0 for _, V: = range nums { diff = diff ^ v } //Get a diff representation Bitwise XOR result of two different numbers diff =-diff & diff Result: = Make ([]int, 2) for _, V: = Range Nums { if diff & v = = 0 { result[0] = result[0] ^ v } else { result[1] = result[1] ^ v } } return result}