classSolution{publicint[]singleNumbers(int[] nums){int x =0, y =0, n =0, m =1;for(int num : nums)// 1. 遍历异或
n ^= num;while((n & m)==0)// 2. 循环左移,计算 m
m <<=1;for(int num: nums){// 3. 遍历 nums 分组if((num & m)!=0) x ^= num;// 4. 当 num & m != 0else y ^= num;// 4. 当 num & m == 0}returnnewint[]{x, y};// 5. 返回出现一次的数字}}
classSolution{publicintsingleNumber(int[] nums){int[] counts =newint[32];for(int num : nums){for(int j =0; j <32; j++){
counts[j]+= num &1;
num >>>=1;}}int res =0, m =3;for(int i =0; i <32; i++){
res +=(1<< i)*(counts[i]% m);}return res;}}