Programmer's Question Selection 100 questions (63)-three occurrences of a number in an array [algorithm]_ sword refers to an offer

Source: Internet
Author: User
Tags diff

Title: An array of three digits A, B, C appears only once, the other numbers appear two times. Please find three digits that appear only once.

Analysis: In the blog http://zhedahht.blog.163.com/blog/static/2541117420071128950682/we discussed how to find two digits that appear only once in an array. In this problem, if we can figure out a number that appears only once, the remaining two digits that appear only once are easy to find.

If we have all the numbers in the array different or not, the final result (recorded as x) is the XOR or result of three numbers A, B, C (x=a^b^c). Other numbers that appear two of times cancel each other out in a different or an operation.

We can prove that the difference or the result x cannot be a, B, c three different numbers of any one. We use contradiction to prove it. Suppose x equals one of a, B, and C. For example, X equals A, which is a=a^b^c. So b^c equals 0, i.e. B equals C. This contradicts the three numbers of a, B, and C that are three distinct.

Because X and a, B, C are all different, so x^a, X^b, x^c is not equal to 0.

We define a function f (n), which is the result of preserving the last 1 of the binary representation of the number n, and turning all the other bits into 0. For example, decimal 6 means that the binary is 0110, so the result of F (6) is 2 (binary is 0010). The results of F (x^a), F (x^b), F (x^c) are not equal to 0.

We then consider the results of F (x^a) ^f (x^b) ^f (x^c). Because only one digit in the binary representation of a result of a non-0 n,f (n) is 1, the result of F (x^a) ^f (x^b) ^f (x^c) is certainly not 0. This is because the result of any three nonzero numbers I, J, K,f (i) ^f (j) is either 0, or two 1 in the binary result of the result. In either case, F (i) ^f (j) cannot be equal to f (k), since F (k) is not equal to 0, and the result of the binary is only one of 1.

So the result of F (x^a) ^f (x^b) ^f (x^c) is at least one of the two binary systems 1. Let's say the last one is 1 bit is the first m bit. So X^a, X^b, X^c, one or three digits of the first M is 1.

Next we proved that the three results of X^a, x^b and x^c could not all be 1. Or with the contradiction proof. If X^a, X^b, and X^c are all 1, then the first m of the three digits of a, B, and C are the opposite, so the first m bits of the three digits of a, B, and C are the same. If a, B, c three numbers of the first m bits are 0,x=a^b^c results of the first m bit is 0. Since the first m bits of x and a two digits are the 0,x^a results the first m bit should be 0. Similarly, it can be proved that x^b, X^c, the first m is 0. This is contradictory to our hypothesis. If a, B, c three numbers of the first m bits are 1,x=a^b^c results of the first m bit is 1. Since the first m bits of x and a two digits are the 1,x^a results the first m bit should be 0. Similarly, it can be proved that x^b, X^c, the first m is 0. This is still contradictory to our assumptions.

So in the three digits of X^a, x^b and x^c, only one digit M is 1. So we found a standard that distinguishes A, B, c three numbers. Of these three digits, only one number meets this standard, while the other two are not. Once the standard numbers are found, the other two numbers can be found.

The C + + code for this idea is as follows:

void Getthreeunique (vector<int>& numbers, vector<int>& unique) {if (Numbers.size () < 3)
   
    Return
    int xorresult = 0;
    Vector<int>::iterator iter = Numbers.begin ();
 
    for (; Iter!= numbers.end (); ++iter) Xorresult ^= *iter;
    int flags = 0;
    for (iter = Numbers.begin (); Iter!= numbers.end (); ++iter) Flags ^= lastBitOf1 (xorresult ^ *iter);
   
    Flags = LASTBITOF1 (flags);
    Get the unique number INT-0;
            for (iter = Numbers.begin (); Iter!= numbers.end (); ++iter) {if (LastBitOf1 (*iter ^ xorresult) = flags)
    ^= *iter;
   
    Unique.push_back (a);
        Move the "the" "The" "the" End of the "Array for" (iter = Numbers.begin (); Iter!= numbers.end (); ++iter) {
            if (*iter = = a) {swap (*iter, * (Numbers.end ()-1));
        Break }//Get the second and third unique numbers GettwouniqUE (Numbers.begin (), Numbers.end ()-1, unique);
 
int lastBitOf1 (int number) {return number & ~ (NUMBER-1);}
void Gettwounique (Vector<int>::iterator begin, Vector<int>::iterator end, vector<int>& unique)
    {int xorresult = 0;
   
    for (vector<int>::iterator iter = begin; Iter!= end; ++iter) Xorresult ^= *iter;
   
    int diff = lastBitOf1 (xorresult);
    int-i = 0;
   
    int second = 0;  for (vector<int>::iterator iter = begin; Iter!= end; ++iter) {if (diff & *iter)-I ^=
        *iter;
    else second ^= *iter;
    Unique.push_back (a);
Unique.push_back (second); }


Getthreeunique finds three occurrences of a single digit from the array, and Gettwounique finds two occurrences of the number in the array. LASTBITOF1 implements the function of function f (n) in the analysis, which retains only the last 1 of the binary representation of the number n, and all other bits become 0.

In function Getthreeunique, we save the results of A, B, c three digits to Xorresult in the first for loop, and then find the F (x^a) ^f (x^b) ^f (x^c) in the second for loop and save it to the variable flags. The last digit in the binary of the result of F (x^a) ^f (x^b) ^f (x^c) in statement FLAGS=LASTBITOF1 (flags) is a bit of 1. And based on this digit, we find the first digit that only appears once. We then swap first to the end of the array and find the other two digits that appear only once in the first n-1 number of the array.

Bo Master He Haitao to this blog article enjoy copyright. The network reprint please indicate the source http://zhedahht.blog.163.com/. Please contact the author to organize your publication.

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.