Problem: In an array of integers except for two digits, the other numbers appear two times. Please write the program to find the two only occurrences of the number. The time complexity is O (n) and the space complexity is O (1).
Analysis: This is a very novel topic about bit arithmetic.
First consider a simple version of this problem: an array of integers in addition to a number, the other numbers appear two times, please write the program to find the only one occurrence of the number.
Where is the breach of this problem? The nature of the array in the topic is that only one integer appears once, and the others appear two times. This makes us think of the nature of the XOR operation: Any number XOR itself equals 0. That is, if you start from beginning to end, or every integer in the array, the result is exactly the one that appears once. Because then 22 of the same integers in the XOR process equals 0 offset.
Based on the above simple solution, go back to the question of the topic. If the original array can be divided into two sub-arrays, each sub-array contains a single occurrence of the number, while the other numbers appear two times. If you can split the original array in this way, then the problem becomes the two arrays in the split to look for the two occurrences of the first number.
So the rest of the question is, according to what conditions to split the score group? The solution is to make all integers different or operation, because the result of the other pairs of integers is equal to 0, so that the final XOR result is equivalent to the two occurrences of the integer XOR, so you want to distinguish between the two integers, you can distinguish between the results of their different or. Find a bit of 1 in their XOR result, which is the nth bit. According to the operating principle of XOR, then the nth bit of the two numbers must be 11 for 0. The entire array is divided into two groups according to the nth bit, which is 1 or 0. can satisfy each sub-array contains a single occurrence of an integer, the other number 22 pairs appear in different sub-arrays.
Algorithm steps:
(1) The result of an XOR operation is obtained by an XOR or an integer in a sequence.
(2) To find the binary form of the result of the XOR operation, the lowest bit of 1 corresponds to the subscript value. Use this bit as a standard for distinguishing two integers.
(3) According to the criteria of differentiation, the entire array is divided into two sub-arrays, respectively, to find their own XOR operation value, and get two results of the operation is required to two occurrences of the integer.
function declaration:
BOOL Findtwosinglenums (intint int int. int &b);
BOOL ISBIT1 (intUINT indexbit);
UINT FINDFIRSTBITIS1 (int num);
Code implementation:
#include <stdio.h>#include<stdlib.h>#include<assert.h>#defineUINT unsigned intBOOLFindtwosinglenums (intNums[],intNlength,int&a,int&b);BOOLIsBit1 (intNumUINTindexbit);UINTFINDFIRSTBITIS1 (intnum);///<summary>///find two integers that cannot be paired in an array///<param name=num>Array name</param>///<param name=nlength>Array Length</param>///<param name=a>First integer reference that cannot be paired</param>///<param name=b>Second integer reference that cannot be paired</param>///<returns>returns BOOL Indicates whether the lookup was successful</return>///</summary>BOOLFindtwosinglenums (intNums[],intNlength,int&a,int&b) { if(Nlength <2) { return false; } intResxor =0; for(inti =0; i < nlength; i++) {Resxor^=Nums[i]; }// for UINTindex =FindFirstBitIs1 (Resxor); for(inti =0; i < nlength; i++) { if(IsBit1 (nums[i], index)) {a^=Nums[i]; } Else{b^=Nums[i]; } }// for}//findtwosinglenums ()///<summary>///find an integer corresponding to the binary number that appears 1 of the lowest bit subscript///<param name=num>integer</param>///<returns>Subscript value for bits that appear 1</return>///</summary>UINTFINDFIRSTBITIS1 (intnum) {Assert (num!=0); UINTindex =0; UINTMaxindex =sizeof(NUM) *8; //note the precedence of operators while(Num &1) ==0) && (Index <Maxindex)) {num>>=1; ++index; }// while returnindex;}//FindFirstBitIs1 ()///<summary>///determines whether an integer corresponds to one of the binary's 1///<param name=num>integer</param>///<param name=indexbit>to judge the subscript of a bit</param>///<returns>return the result of judgment</return>///</summary>BOOLIsBit1 (intNumUINTindexbit) {num>>=Indexbit; returnNum &1;}//IsBit1 ()
Test code:
void Main ()
{
int array[16] = {51,52,78,45,89,54,12,70,
51,52,78,45,89,54,12,74};
Initialized to 0, or it would be a random number and an array of XOR operations
0 and any number XOR or operation equals its own
int onesinglenum = 0;
int twosinglenum = 0;
Findtwosinglenums (Array, onesinglenum, twosinglenum);
printf ("%d--%d\n", Onesinglenum, twosinglenum);
System ("pause");
}
PS: The same number or the result is 0, and the array is divided into two parts according to the position of the first occurrence of all the elements, or 1 (the lowest bit of 1 appears).
Algorithm problem: Finding two occurrences of a number in an array of integers