# 程序员面试题精选100题(34)-数组中只出现一次的数字[算法]

///////////////////////////////////////////////////////////////////////
// Find two numbers which only appear once in an array
// Input: data - an array contains two number appearing exactly once,
//               while others appearing exactly twice
//        length - the length of data
// Output: num1 - the first number appearing once in data
//         num2 - the second number appearing once in data
///////////////////////////////////////////////////////////////////////
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];
}
}

///////////////////////////////////////////////////////////////////////
// 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);

}

class Solution {
public:
void FindNumsAppearOnce(vector<int> data,int* num1,int *num2) {
int length = data.size();
if (length < 2)
{
return;
}
int temp = 0;
int flag = 1;
int i;
for (i = 0; i < length; i++)
{
temp ^= data[i];
}
while ((temp & flag) == 0)//与操作注意括号
{
flag = flag << 1;//注意
}
*num1 = 0;
*num2 = 0;
for (i = 0; i < length; i++)
{
if ((data[i] & flag) == 0)
{
*num1 ^= data[i];
}
else
{
*num2 ^= data[i];
}
}

}
};

• 本文已收录于以下专栏：

举报原因： 您举报文章：程序员面试题精选100题(34)-数组中只出现一次的数字[算法] 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)