public class Test1 {
public static void main(String[] args) {
int[] a = {1, 2, 2, 3, 3, 7, 7, 0, 0, 6, 6, 9};
int sum = getSum(a);
int number = 0;
for (int j = 1; j < 32; j = j << 1) {
if ((sum & j) == j) {
number = j;
break;
}
}
int[] b = new int[a.length];
int[] c = new int[a.length];
int x = 0;
int y = 0;
if (number != 0) {
for (int j = 0; j < a.length; j++) {
if ((a[j] & number) == number) {
b[x++] = a[j];
} else {
c[y++] = a[j];
}
}
int sum1 = getSum(b);
int sum2 = getSum(c);
System.out.println(sum2);
System.out.println(sum1);
}
}
//将数组中的数据进行异或
public static int getSum(int a[]) {
int i = 1;
int sum = a[0];
while (i < a.length) {
sum ^= a[i];
i++;
}
return sum;
}
}
算法-01 求数组中出现奇数次的两个数
最新推荐文章于 2022-06-12 23:23:40 发布