Topic:
1, given an array, the array of numbers are of type int, in addition to one number appears once, others appear two times, please find this number;
2, given an array, the array of numbers are of type int, in addition to one number appears once, others appear three times, please find this number;
Ideas:
The easiest way to think about these two problems is by hashmap statistics or by sorting them out, but they either require a high degree of space complexity or a high degree of time complexity.
Is there a method that has a constant spatial complexity and a time complexity of O (n)?
In fact, both problems can be obtained by the simple method of bitwise operation.
Topic 1:
The same number is different or equal to 0, so all the numbers in the array are different or manipulated, and the resulting number is the one that does not recur.
This thought can be applied to: A number appears once, others appear even several times.
Topic 2:
Method 1:
Creates an array of length sizeof (int) count[sizeof (int)],count[i] that represents the number of occurrences of 1 in the I bit. If Count[i] is an integer multiple of 3, it is ignored, otherwise the bit is taken out to form an answer.
Method 2:
With one recording to the currently processed element, the binary 1 has the "1" (1 after mod 3) bits, and the binary 1 appears "2 times" (3 after mod 2) for the bits of the current computed variable. When one and two are at the same time 1 indicates that the bits on the 1 appears 3 times, this time need to clear zero. Binary is used to simulate the binary operation. The final one records the final result.
Code:
Topic 1:
int Singlenumberi (int* a,int n) { int single=0; for (int i=0;i<n;i++) single^=a[i]; return single;}
Topic 2:
int SINGLENUMBERIII (int* a,int N) { const int num=32; int Count[num]; Fill_n (&count[0],num,0); memset (count,0,num*sizeof (int)); for (int i=0;i<n;i++) {for (int j=0;j<num;j++) { count[j]+= ((a[i]>>j) &1); count[j]%=3; } } int result=0; for (int i=0;i<num;i++) { if (count[i]==1) result+= (1<<i); } return result;} int Singlenumberii (int* a,int n) { int one=0; int two=0; int three=0; for (int i=0;i<n;i++) { two|=one&a[i]; One^=a[i]; three=~ (one&two); one&=three; two&=three; } return one;}
The total code:
#include <iostream> #include <string.h>using namespace Std;int singlenumberi (int* a,int n) {int single=0; for (int i=0;i<n;i++) single^=a[i]; return single;} int SINGLENUMBERIII (int* a,int n) {const int num=32; int Count[num]; Fill_n (&count[0],num,0); memset (count,0,num*sizeof (int)); for (int i=0;i<n;i++) {for (int j=0;j<num;j++) {count[j]+= ((a[i]>>j) &1); count[j]%=3; }} int result=0; for (int i=0;i<num;i++) {if (count[i]==1) result+= (1<<i); } return result; int Singlenumberii (int* a,int n) {int one=0; int two=0; int three=0; for (int i=0;i<n;i++) {two|=one&a[i]; One^=a[i]; three=~ (One&two); one&=three; two&=three; } return one; int main () {int a[]={2,3,4,3,2,5,5}; int lena=sizeof (A)/sizeof (a[0]); cout << singlenumberi (A,lena) << Endl; int b[]={1,4,1,1,5,5,5,7,7,7}; int lenb=sizeof (B)/sizeof (b[0]); cout << singlenumberii (B,LENB) << Endl; cout << singlenumberiii (B,LENB) << Endl; return 0;}
(written question) only one occurrence of the number