/**
* Author: Taoyongpan
* Date: Created in 9:35 2018/6/14
* 一個數(shù)組中只有一個數(shù)只出現(xiàn)了奇數(shù)次甚疟,其他都是出現(xiàn)偶數(shù)次茫陆,時間復(fù)雜度為O(n)
*/
public class Test10 {
public static void main(String[] args) {
int[] arr = {1,1,2,3,4,2,3,};
int res = arr[0];
for (int i = 1 ; i< arr.length;i++){
res^=arr[i];
}
System.out.println(res);
}
}
/**
* Author: Taoyongpan
* Date: Created in 9:42 2018/6/14
* 數(shù)組中只出現(xiàn)一次的兩個數(shù)字
* 方法金麸,將 兩個不同的數(shù)分別放置到兩個數(shù)組中,
* 通過二進(jìn)制的某一位簿盅,是0或者1將整個數(shù)組拆分開
*/
public class Test12 {
public static void main(String[] args) {
int[] arr = {1,1,2,3,4,2,};
int res = 0;
int j = 0;
int num1 = 0;
int num2 = 0;
for (int i = 0 ; i < arr.length ; i++ ){
res^=arr[i];
}
for (j = 0 ;j < 32;j++){
if (((res>>j)&0)==0){
break;
}
}
for (int i = 0;i<arr.length;i++){
if (((arr[i]>>j)&1)==1)
num1^=arr[i];
else
num2^=arr[i];
}
System.out.println(num1);
System.out.println(num2);
}
}