Sword refers to only one occurrence of a number in an offer array

Source: Internet
Author: User

The title describes an array of integers in addition to the two numbers, the other numbers appear two times. Please write the program to find the two only occurrences of the number. Idea: Because there are 2 numbers that only appear once, and the other numbers are 2 times, you can get the difference or the result value of the last 2 occurrences of the number by XOR operation. This value must not be 0. Then find out this number binary, the lowest bit is 1 of the number of bits, it is necessary that one is 0, one is 1. By this condition, the original array is divided into 2 parts, respectively, and the result is obtained. Code:
1 classSolution {2  Public:3     voidFindnumsappearonce (vector<int> Data,int* NUM1,int*num2)4     {5         intres =0, tag =0;6         if(Data.empty ())7         {8NUM1 = Num2 =0;9         }Ten  One          for(inti =0; I < data.size (); i++) A         { -Res ^=Data[i];  -         } the  -          for(; tag < +; tag++) -         { -             if(Res & (1<<tag))! =0) +                  Break; -         } +  A          for(inti =0; I < data.size (); i++) at         { -             if((data[i) & (1<< tag) = =0) -*num1 ^=Data[i]; -             Else -*num2 ^=Data[i]; -         } in     } -};

Question 2:

There is one number in an array that appears only once.

Ideas:

Directly traversing an XOR or an entire array, you get the number that appears only once.

Question 3:

In an array, a number appears 1 times, and the others appear 3 times

Ideas:

Open up an array of digits, traverse the far array to record 2 of all elements, and the number of bits.

Iterate through the array of bits, divide the value by 3, and the last array of digits left is the last one that appears only once.

Code:

1  Public Static intFIND1FROM3 (int[] a) {2         int[] bits =New int[32];3         intLen =a.length;4          for(inti = 0; i < Len; i++){5              for(intj = 0; J < 32; J + +){6BITS[J] = Bits[j] + ((a[i]>>>j) & 1);7             }8         }9         intres = 0;Ten          for(inti = 0; I < 32; i++){ One             if(Bits[i]% 3!=0){ Ares = Res | (1 << i);

Sword refers to only one occurrence of a number in an offer array

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.