Link
New Ket OJ: A number that appears only once in an array
Nine degrees oj:http://ac.jobdu.com/problem.php?pid=1351
GitHub code: 040-only one occurrence of a number in an array
Csdn: Sword refers to a number that appears only once in the offer–040-array
New Ket OJ |
Nine degrees OJ |
csdn |
GitHub Code |
040-A number that appears only once in an array |
1351-A number that appears only once in an array |
Sword refers to a number that appears only once in the offer–040-array |
040-A number that appears only once in an array |
Test instructions
Title Description
In an array of integers, except for two digits, the other numbers appear two times. Please write the program to find the two only occurrences of the number
2 4 3 6 3 2 5 5
Sample output
4 6
Analysis
This topic examines the characteristics of an XOR operation: that is, two identical numbers are different or the result is 0.
This topic uses two different or operation characteristics:
The first time you use an XOR operation, you get two different or different results that occur only once.
Because two only one occurrence of the number must be different, that is, their differences or results must not be 0, there must be a bit on 1.
The other one does not have 1 on this bit, we can divide the whole array into two parts according to whether there is 1 on this bit,
Part of this place must have 1, another part of this bit must not be 1,
Then each part of the difference or, because the division of the two parts have such characteristics: the other number appears two times, only one number appears once. Therefore, we can use the XOR operation, respectively, to get the two parts only one occurrence of the number.
Code
#include <iostream>#include <vector>#include <bitset>using namespace STD;#define __tmain Main#ifdef __tmain#define DEBUG Cout#else#define DEBUG 0 && cout#endif //__tmain classsolution{ Public:#define INT_SIZE (sizeof (INT) * 8) #define IS_BIT (number, index) ((number) & (1 << (index))) >>index) voidFindnumsappearonce ( vector<int> Array,int*NUM1,int*NUM2) {*num1 = *num2 =0;if(Array. Size () <2) {return; }intXOR =Array[0]; for(inti =1; I <Array. Size (); i++) {XOR ^=Array[i]; } Debug << bitset<INT_SIZE>(XOR) <<endl;/// Find the location to 1 intindex =0, temp =1; for(; index < int_size; index++) {if(XOR & temp) = = temp) { Break; } temp <<=1; } Debug <<index <<endl;if(index = = int_size) {Debug <<"No, numbers which once"<<endl;return; } Debug <<"Find 1 in index ="<<index <<endl; Debug <<is_bit (XOR, index) <<endl;///So we know that the two appear only once the number, the index position must be different ///So we divide the array into two parts //Part of index bit is 0 //The other part of the index bit is 1*NUM1 = *num2 =0;//can also be equal to XOR for(inti =0; I <Array. Size (); i++) {if(Is_bit (Array[i], index) = =1) {*num1 ^=Array[i]; }Else{*num2 ^=Array[i]; } } }};int__tmain () {solution Solu;intArr[] = {2,4,3,6,3,2,5,5, }; vector<int>VEC (arr, arr +8);intNUM1, num2; Solu. Findnumsappearonce (VEC, &NUM1, &num2);cout<<"RESULT:"<<num1 <<", "<<num2 <<endl;return 0;}
When looking for the last 1 position in XOR, you can use xor & (-xor)
Since the internal integers of the computer are stored in complement, the complement is represented by:
A positive complement is its own
The complement of a negative number is in its original code (the original code is the absolute value of the symbol bit plus the true value, that is, with the first representation of the symbol, the remaining bits represent values.) on the basis of the symbol bit unchanged, the rest of you take the reverse, the last +1. (That is, on the basis of the anti-code +1)
The simpler method is that the sign bit is unchanged, starting from the lowest bit, the guide meets the first 1, the 1 is unchanged, and all the preceding bits are reversed in turn.
So we will find that the difference between XOR and-xor is the same as the first 1 of the lowest starting point, and then the previous bit position is exactly the opposite.
So the XOR & (-xor) value is just 1, and this position is exactly the position of the XOR lowest bit 1.
Code
classsolution{ Public:#define INT_SIZE (sizeof (INT) * 8) #define IS_BIT (number, index) ((number) & (1 << (index))) >>index) voidFindnumsappearonce ( vector<int> Array,int*NUM1,int*NUM2) {*num1 = *num2 =0;if(Array. Size () <2) {return; }intXOR =Array[0]; for(inti =1; I <Array. Size (); i++) {XOR ^=Array[i]; } Debug << bitset<INT_SIZE>(XOR) <<endl;/// Find the location to 1 intFlag = XOR & (-xor); Debug << bitset<INT_SIZE>(flag) <<endl; *NUM1 = *num2 = XOR;//can also be equal to XOR for(inti =0; I <Array. Size (); i++) {if((Array[i] & flag) = = Flag) {*NUM1 ^=Array[i]; }Else{*num2 ^=Array[i]; } } }};
Sword refers to a number that appears only once in the offer--040-array