This article address: http://blog.csdn.net/morewindows/article/details/12684497 reprint Please indicate the source, thank you.
Welcome to follow Weibo: Http://weibo.com/MoreWindows
First look at the topic requirements:
In array A, except for a number x, the other numbers appear three times, and X appears once. Please give the quickest way to find X.
This topic is very interesting, in my blog has the "bit operation basics of the operation of a comprehensive summary" This article describes the use of bit operation XOR to solve--the array of other numbers appear two times, and X appears once, find X. There are only 1 times in the "Vernacular classic algorithm series 12" array of two numbers (Baidu interview) "This article describes the method of grouping XOR--the other numbers in the array appear two times, and X and y appear once, find X and Y." The problem is that the other numbers appear 3 times and X appears once.
How should you think?
The first two articles are calculated using two identical numbers or results of zero, but the other numbers in this topic are 3 times, so it's definitely not possible to use XOR.
We look at a different angle, if there is no x in the array, then all the numbers in the array appear 3 times, on the binary, the number of 1 on each bit must also be divisible by 3. such as {1, 5, 1, 5, 1, 5} from the binary view are:
1:0001
5:0101
1:0001
5:0101
1:0001
5:0101
The binary No. 0 bit has 6 1, 2nd bit has 3 1.1th and 3rd are 0 1, and each statistic can be divisible by 3. Adding any number to the array, if the number is 1 on a bit of binary, will cause the number of 1 on that bit to be divisible by 3. So by counting the number of 1 per digit on the binary, it is possible to infer whether X is 0 or 1 at that position, so that x can be calculated.
Promotion, all other numbers appear N (n>=2) times, and a number appears 1 times can use this solution to deduce the occurrence of this 1 times the number.
The sample code is as follows:
[CPP]View Plaincopy
- "Vernacular Classic Algorithm series 17" <span > array only one occurrence in the number </span>
- by Morewindows (http://blog.csdn.net/MoreWindows)
- Please pay attention to http://weibo.com/morewindows
- #include <stdio.h>
- #include <string.h>
- int Findnumber (int a[], int n)
- {
- int bits[32];
- int I, J;
- //bits of all numbers in the array
- memset (Bits, 0, * sizeof (int));
- For (i = 0; i < n; i++)
- For (j = 0; J <; J + +)
- BITS[J] + = ((A[i] >> j) & 1);
- //If the result on a bit cannot be divisible, then the target number must be
- int result = 0;
- For (j = 0; J <; J + +)
- if (Bits[j]% 3! = 0)
- Result + = (1 << j);
- return result;
- }
- int main ()
- {
- printf ("" "in the Vernacular classic algorithm series 17" Only one occurrence of the number \ n ");
- printf ("-by Morewindows (http://blog.csdn.net/MoreWindows)--\n");
- printf ("--http://blog.csdn.net/morewindows/article/details/12684497--\ n");
- const int maxn = 10;
- int A[MAXN] = {2, 3, 1, 2, 3, 4, 1, 2, 3, 1};
- printf ("%d\n", Findnumber (A, MAXN));
- return 0;
- }
The results of the operation are as follows:
This article address: http://blog.csdn.net/morewindows/article/details/12684497 reprint Please indicate the source, thank you.
Welcome to follow Weibo: Http://weibo.com/MoreWindows
"Vernacular Classic Algorithm series 17" Only one occurrence in the array of the other three times