题目描述
一个整型数组里除了两个数字之外,其他的数字都出现了偶数次。请写程序找出这两个只出现一次的数字。
import java.lang.Math;
//num1,num2分别为长度为1的数组。传出参数
//将num1[0],num2[0]设置为返回结果
public class Solution {
public void FindNumsAppearOnce(int [] array,int num1[] , int num2[]) {
int temp = 0;
for(int i=0;i<array.length;i++){
temp ^= array[i];
}
int bitlocation = 0;
while(bitlocation<32){
if((temp & 1)==1){
break;
}
else{
temp = temp >> 1;
bitlocation++;
}
}
int bijiao = (int)Math.pow(2,bitlocation);
for(int i=0;i<array.length;i++){
if((array[i]&bijiao)==0){
num1[0] ^= array[i];
}
else{
num2[0] ^= array[i];
}
}
}
}