題目:
Given an array of numbers nums
, 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:
Given nums = [1, 2, 1, 3, 2, 5]
, return [3, 5]
.
Note:
[5, 3]
is also correct.public class Solution { public int[] singleNumber(int[] nums) { int[] single = new int[2]; int[] wei = new int[32]; int yhResult = 0; for(int i=0; i//如果異或結果的某位為1,說明兩個單數在該位是不同的 for(int j=0; j //按該位相同與不同,分兩組分別異或,可直接得出結果元素 single[0] ^= nums[j]; else single[1] ^= nums[j]; } break; } } return single; } }