Q在一个数组中除两个数字只出现1次外,其它数字都出现了2次, 要求尽快找出这两个数字。
设题目中这两个只出现1次的数字分别为A和B,如果能将A,B分开到二个数组中,那显然符合“异或”解法的关键点了。因此这个题目的关键点就是将A,B分开到二个数组中。由于A,B肯定是不相等的,因此在二进制上必定有一位是不同的。根据这一位是0还是1可以将A,B分开到A组和B组。而这个数组中其它数字要么就属于A组,要么就属于B组。再对A组和B组分别执行“异或”解法就可以得到A,B了。而要判断A,B在哪一位上不相同,只要根据A异或B的结果就可以知道了,这个结果在二进制上为1的位都说明A,B在这一位上是不相同的。
比如int a[] = {1, 1, 3, 5, 2, 2}
整个数组异或的结果为3^5即 0x0011 ^ 0x0101 = 0x0110
对0x0110,第1位(由低向高,从0开始)就是1。因此整个数组根据第1位是0还是1分成两组。
a[0] =1 0x0001 第一组
a[1] =1 0x0001 第一组
a[2] =3 0x0011 第二组
a[3] =5 0x0101 第一组
a[4] =2 0x0010 第二组
a[5] =2 0x0010 第二组
第一组有{1, 1, 5},第二组有{3, 2, 3},明显对这二组分别执行“异或”解法就可以得到5和3了。
// 百度面试题
//数组中除两个数字外,其它数字都出现了次。要求尽可能快的找出这两个数字
//By MoreWindows (http://blog.csdn.net/MoreWindows)
#include <stdio.h>
void FindTwoNotRepeatNumberInArray(int *a, int n, int *pN1, int *pN2)
{
int i, j, temp;
//计算这两个数的异或结果
temp = 0;
for (i = 0; i < n; i++)
temp ^= a[i];
// 找第一个为1的位
for (j = 0; j < sizeof(int) * 8; j++)
if (((temp >> j) & 1) == 1)
break;
// 第j位为1,说明这两个数字在第j位上是不相同的
// 由此分组即可
*pN1 = 0, *pN2 = 0;
for (i = 0; i < n; i++)
if (((a[i] >> j) & 1) == 0)
*pN1 ^= a[i];
else
*pN2 ^= a[i];
}
void PrintfArray(int a[], int n)
{
for (int i = 0; i < n; i++)
printf("%d ", a[i]);
putchar('\n');
}
int main()
{
printf(" 白话经典算法系列之十二数组中不重复的个数字(百度面试题) \n");
printf(" -- by MoreWindows( http://blog.csdn.net/MoreWindows ) --\n\n");
const int MAXN = 10;
//int a[MAXN] = {1, 2, 7, 5, 100, 100, 6, 1, 2, 5};
int a[MAXN] = {1, 2, 3, 4, 1, 2, 3, 4, 0, 5};
printf("数组为: \n");
PrintfArray(a, MAXN);
int nNotRepeatNumber1, nNotRepeatNumber2;
FindTwoNotRepeatNumberInArray(a, MAXN, &nNotRepeatNumber1, &nNotRepeatNumber2);
printf("两个不重复的数字分别为: %d %d\n", nNotRepeatNumber1, nNotRepeatNumber2);
return 0;
}
思路点拨:现在以第N位是不是1为标准把原数组中的数字分成两个子数组,第一个子数组中每个数字的第N位都为1,而第二个子数组的每个数字的第N位都为0。
#include <iostream>
using namespace std;
// Find the index of first bit which is 1 in num (assuming not 0)
unsigned int FindFirstBitIs1(int num)
{
int indexBit = 0;
while (((num & 1) == 0) && (indexBit < 32))
{
num = num >> 1;
++ indexBit;
}
return indexBit;
}
// Is the indexBit bit of num 1?
bool IsBit1(int num, unsigned int indexBit)
{
num = num >> indexBit;
return (num & 1);
}
// Find two numbers which only appear once in an array
// Input: data - an array contains two number appearing exactly once,
void FindNumsAppearOnce(int data[], int length, int &num1, int &num2)
{
if (length < 2) return;
// get num1 ^ num2
int resultExclusiveOR = 0;
for (int i = 0; i < length; ++ i)
resultExclusiveOR ^= data[i];
// get index of the first bit, which is 1 in resultExclusiveOR
unsigned int indexOf1 = FindFirstBitIs1(resultExclusiveOR);
num1 = num2 = 0;
for (int j = 0; j < length; ++ j)
{
// divide the numbers in data into two groups,
// the indexOf1 bit of numbers in the first group is 1,
// while in the second group is 0
if(IsBit1(data[j], indexOf1))
num1 ^= data[j];
else
num2 ^= data[j];
}
}
int main()
{
int a[8] = {2,3,6,8,3,2,7,7};
int x,y;
FindNumsAppearOnce(a,8,x,y);
cout<<x<<"\t"<<y<<endl;
return 0;
}
参考
http://blog.csdn.net/morewindows/article/details/8214003
http://jishublog.iteye.com/blog/1942634
http://www.cnblogs.com/luxiaoxun/archive/2012/09/08/2676610.html

浙公网安备 33010602011771号