"Vernacular Classic Algorithm series 17" Only one occurrence in the array of the other three times

Source: Internet
Author: User

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
  1. "Vernacular Classic Algorithm series 17" <span > array only one occurrence in the number </span>
  2. by Morewindows (http://blog.csdn.net/MoreWindows)
  3. Please pay attention to http://weibo.com/morewindows
  4. #include <stdio.h>
  5. #include <string.h>
  6. int Findnumber (int a[], int n)
  7. {
  8. int bits[32];
  9. int I, J;
  10. //bits of all numbers in the array
  11. memset (Bits, 0, * sizeof (int));
  12. For (i = 0; i < n; i++)
  13. For (j = 0; J <; J + +)
  14. BITS[J] + = ((A[i] >> j) & 1);
  15. //If the result on a bit cannot be divisible, then the target number must be
  16. int result = 0;
  17. For (j = 0; J <; J + +)
  18. if (Bits[j]% 3! = 0)
  19. Result + = (1 << j);
  20. return result;
  21. }
  22. int main ()
  23. {
  24. printf ("" "in the Vernacular classic algorithm series 17" Only one occurrence of the number \ n ");
  25. printf ("-by Morewindows (http://blog.csdn.net/MoreWindows)--\n");
  26. printf ("--http://blog.csdn.net/morewindows/article/details/12684497--\ n");
  27. const int maxn = 10;
  28. int A[MAXN] = {2, 3, 1, 2, 3, 4, 1, 2, 3, 1};
  29. printf ("%d\n", Findnumber (A, MAXN));
  30. return 0;
  31. }

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.