The classical algorithm (12) only appeared 1 times in the array of two digits (Baidu interview question)

Source: Internet
Author: User
Tags arrays printf

First look at the topic requirements:

In an array, except for the two digits appearing only 1 times, the other numbers appear 2 times, requiring that the two digits be identified as soon as possible.

Consider the simplified version of the topic--except for a number that appears only 1 times in the array--the other numbers appear in pairs, requiring that the number be found as quickly as possible. This topic has been answered in the "bit-action-interesting application" in the previous "bit Operation Basics" Overview. According to the characteristics of the XOR or operation, the number can be found directly or once.

Now there are two digits in the array that only appear 1 times, directly or at a time can only get the differences or results of these two digits, but the light from this result certainly cannot get this two digits. So let's analyze the key point of the "XOR" solution in the simplified version, which is also quite obvious--an array can only have one number 1 times.

The two numbers that only appear 1 times in the topic are A and B, and if you can separate the a,b into two arrays, it is clearly in line with the key point of the "XOR" solution. So the key point of this topic is to separate the a,b into two arrays. Since a,b is certainly not equal, there must be a difference in the binary. Depending on whether this is 0 or 1, a,b can be separated into Group A and group B. The other numbers in this array either belong to group A or belong to Group B. Then we can get a and B by performing the "XOR" solution for Group A and Group B respectively. And to judge A, b in which is not the same, as long as the result of a or b can be known, this result in the binary 1 of the bits indicate that a,b on this one is not the same.

For example int a[] = {1, 1, 3, 5, 2, 2}

The entire array will have the same result as 3^5 or 0x0011 ^ 0x0101 = 0x0110

For 0x0110, the 1th digit (from low to high, starting from 0) is 1. So the entire array is divided into two groups according to the 1th digit 0 or 1.

A[0] =1 0x0001 First Group

A[1] =1 0x0001 First Group

A[2] =3 0x0011 Group II

A[3] =5 0x0101 First Group

A[4] =2 0x0010 Group II

A[5] =2 0x0010 Group II

The first group has {1, 1, 5}, the second group has {3, 2, 3}, obviously to these two groups to perform the "different or" solution can get 5 and 3.

Analysis to some, believe that the code is not difficult to write, the following gives a complete source code:

//Baidu Face questions//array except two digits, all the other numbers appear. //by morewindows (http://blog.csdn.net/MoreWindows) #include <stdio.h> void Findtwonotrepeatnum are required as soon as possible to locate these two digits  
          
    Berinarray (int *a, int n, int *pn1, int *pn2) {int I, j, temp;  
    Calculate the XOR or result of these two numbers temp = 0;  
          
    for (i = 0; i < n; i++) temp ^= a[i]; Find the first bit for 1 (j = 0; j < sizeof (int) * 8; j + +) if ((temp >> j) & 1) = 1) b  
      
    Reak;  
    The J bit is 1, which indicates that the two digits are not the same on the J-bit so that the group can *PN1 = 0, *pn2 = 0;  
        for (i = 0; i < n; i++) if (((a[i) >> j) & 1) = = 0) *pn1 ^= a[i];  
else *PN2 ^= A[i];  
    } void Printfarray (int a[], int n) {for (int i = 0; i < n; i++) printf ("%d", a[i]);  
Putchar (' \ n ');        
    int main () {printf ("A number not duplicated in the 12 array of the Vernacular classic Algorithm series" (Baidu interview question) \ n "); printf ("--by Morewindows (http://blog.csdn.net/MoreWindows)--\n\n ");  
    const int MAXN = 10;  
    int A[MAXN] = {1, 2, 7, 5, 100, 100, 6, 1, 2, 5};  
      
    int A[MAXN] = {1, 2, 3, 4, 1, 2, 3, 4, 0, 5};  
    printf ("Array: \ n");  
      
    Printfarray (A, MAXN);  
    int NNotRepeatNumber1, NNotRepeatNumber2;  
    Findtwonotrepeatnumberinarray (A, MAXN, &nnotrepeatnumber1, &nnotrepeatnumber2);  
    printf ("Two distinct numbers are:%d%d\n", NNotRepeatNumber1, NNotRepeatNumber2);  
return 0; }

The results of the run are as follows:

Baidu interview in the algorithm this link generally will be a number of algorithm problems, the next time to organize a few, welcome to continue to see.

Original address: http://blog.csdn.net/morewindows/article/details/8214003

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.