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?
Credits:
Special thanks to @jianchao. Li.fighter for adding the problem and creating all test cases.
1 /**2 * This code is provided by the nine-chapter algorithm editor. No copyright welcome forwarding. 3 *-Nine chapters The algorithm is committed to helping more Chinese to find a good job, the teachers team from Silicon Valley and the domestic first-line large companies in-service engineers. 4 *-existing interview training courses include: Nine Chapters algorithm class, System design class, bat domestic class5 *-For more details, please visit the official website:http://www.jiuzhang.com/6 */7 8 Public classSolution {9 /**Ten * @paramA:an Integer Array One * @return: Integers A */ - PublicList<integer> SINGLENUMBERIII (int[] A) { - intXOR = 0; the for(inti = 0; i < a.length; i++) { -XOR ^=A[i]; - } - + intLastbit = XOR-(XOR & (xor-1)); - intGroup0 = 0, group1 = 0; + for(inti = 0; i < a.length; i++) { A if((Lastbit & a[i]) = = 0) { atGroup0 ^=A[i]; -}Else { -Group1 ^=A[i]; - } - } - inarraylist<integer> result =NewArraylist<integer>(); - Result.add (group0); to Result.add (group1); + returnresult; - } the}
*single number