Rough gave the analysis, recently tired, will be improved later. The topic includes three small questions, from simple to complex: 1, if only one appears once, to investigate the nature of the XOR, is if the same number and their own or the result of the work is zero, then loop through the array, the elements in the array to do all the different or operation, then two times the number is all different or dropped, The result is the number that appears only once. 2, if there are two only one occurrence of the number, set to a, B. It is also a result of applying XOR, but all the different elements of an array element are x=a^b, because a, B is not the same number, so X is definitely not 0. For x, starting from low to high, find that the first bit of bit 1 is set to the M-bit, the bit m bit must come from a or B, it is not possible at the same time, the M-bit (from low to high) of a B is 1. In this way, the array can be divided into two parts according to the M-bit, with a group of M bits of 0 and a group of 1 m bits. Thus, the problem is decomposed into a number that only appears once in two arrays. The following first gives this part of the program and example analysis:
[CPP]View Plaincopy
- <span style="FONT-FAMILY:FANGSONG_GB2312;FONT-SIZE:14PX;" > #include <stdio.h>
- int get_first_position (int num)
- {//start from low to find the first bit bit 1
- int index=1;
- int i=0;
- While (i<32)
- {
- if ((num& (1<<i)) = = (1<<i))
- Break ;
- Else
- {
- index++;
- i++;
- }
- }
- return index;
- }
- int is_bit_one (int num,int index)
- {//Determines whether the bit bit at the given index position is 1
- Num= (Num>>index);
- return num&1;
- }
- void Get_two_unique_num (int *a,int n,int *num1,int *num2)
- {
- int exclusive_or_result=0;
- *num1=0;
- *num2=0;
- For (int i=0;i<n;i++)
- Exclusive_or_result^=a[i];
- int index=get_first_position (exclusive_or_result);
- For (i=0;i<n;i++)
- if (Is_bit_one (a[i],index))
- (*NUM1) ^=a[i];
- For (i=0;i<n;i++)
- if (!is_bit_one (a[i],index))
- (*num2) ^=a[i];
- }
- void Main ()
- {
- int a[]={2,2,4,4,6,6,3,5};
- int num1,num2;
- Get_two_unique_num (A,sizeof (a)/sizeof (int), &num1,&num2);
- printf ("%d\t%d\n", num1,num2);
- }</span>
3, considering that there are three separate numbers in a given array, this is slightly more complex than the two. Step analysis, set these three numbers to A,b,c: (1) The number in the array is all different or the result x=a^b^c, but X is not one of a,b,c, assuming x=a, then b^c=0 description b=c, with the topic given the conditions of contradiction. (2) Set F (n) can be as in 2, starting from the low, find the first bit 1 position, F (x^a), F (x^b), F (x^c) to get the value is certainly not 0, because X^a,x^b,x^c itself is not 0. F (x^a) ^f (x^b) ^f (x^c) result is not 0. Because the result of F (x^a) ^f (x^b) may be 0, there may be two bit 1. If you assume that the result of the bit 1 of f (x^c) is coincident with one of the F (x^a) ^f (x^b), then the result of f (x^a) ^f (x^b) ^f (X^C) is only 1 bit 1, and if not coincident then there are 3 bit bits 1. (3) This can infer that at least one bit bit in F (x^a) ^f (x^b) ^f (x^c) is 1. The Mbit bit from low to high is assumed to be 1. Then you can conclude that there is one or three of the x^a,x^b,x^c in the M-bit 1 (there is no two, because there are two, the outcome of the XOR is 0). (4) It is proved that only one m-bit bit in x^a,x^b,x^c is 1. Assuming that their m-bit is 1, then x is 0, but the x=a^b^c is definitely 1, so the hypothesis is not true. On the contrary, it is assumed that the 1,a,b,c m bit of x is 0 and is not true because of the x=a^b^c. So in summary x^a,x^b,x^c only one of the first m bit is 1. Then this is a good question. Find the first number that appears only once, according to the M-bit. Then the remaining two is the problem described in question 2. The following code is given:
[CPP]View Plaincopy
- #include <stdio.h>
- int get_first_bit (int num)
- {
- return num&~ (num-1);
- }
- void Get_two_unique_num (int *a,int n,int *num1,int *num2)
- {
- int result_code=0;
- For (int i=0;i<n;i++)
- Result_code^=a[i];
- int diff=get_first_bit (result_code);
- *num1=0;
- *num2=0;
- For (i=0;i<n;i++)
- {
- if (A[i]&diff)
- {
- (*NUM1) ^=a[i];
- }
- Else
- {
- (*num2) ^=a[i];
- }
- }
- }
- void Get_three_unique_num (int *a,int n,int *num1,int *num2,int *num3)
- {
- int result_code=0;
- For (int i=0;i<n;i++)
- Result_code^=a[i];
- int flag=0;
- For (i=0;i<n;i++)
- Flag^=get_first_bit (Result_code^a[i]);
- Flag=get_first_bit (flag);
- *num1=0;
- For (i=0;i<n;i++)
- {
- if (Get_first_bit (Result_code^a[i]) ==flag)
- {
- (*NUM1) ^=a[i];
- }
- }
- For (i=0;i<n;i++)
- {
- if (a[i]== (*NUM1))
- {
- int temp=a[i];
- A[I]=A[N-1];
- A[n-1]=temp;
- Break ;
- }
- }
- Get_two_unique_num (A,N-1,NUM2,NUM3);
- }
- void Main ()
- {
- int a[]={2,2,4,4,6,6,3,5,7};
- int num1,num2,num3;
- Get_three_unique_num (A,sizeof (a)/sizeof (int), &NUM1,&NUM2,&NUM3);
- printf ("%d\t%d\t%d\n", num1,num2,num3);
- }
In an integer array, except for one or two or three digits, the other numbers appear two times. Please write the program to find the two only occurrences of the number. Required time complexity is O (n), spatial complexity is O (1)