Find the only number O (n) bit arithmetic that appears 1 times without appearing three times

Source: Internet
Author: User

The previous two times that was XOR or arithmetic processing. This time I thought it was similar. But I didn't think of it.
Gaofu think out of the algorithm, to Bitset, and then add up the same words either 0+0+0 or 1+1+1, the last remaining can be calculated by%3 0 or 1. Thought is like this,
is actually a bit operation. Just not XOR or such an operation O (1) such, but because the input is an int array, -2^31~2^31-1 so with 32bit can be expressed.


Previous encounters, several errors, contain the problem of allocating storage space, as Fawks said. Using global arrays, is in the global area, much larger than the stack space. So I can apply for a large array, but Leetcode always
Do not give data range, just from int can also know, but Leetcode is class, public members seems to be the stack. The result is an error. By the way, Leetcode very much WA is said to be tle.

。 There are other types that specify errors.




Later found that there is a negative question, negative modulus symbol bit is different or ( -7/-4=1.....-3, -7/4=-1....-3, 7/-4=-1.....3, 7/4=1....3 so can also be summed up, quotient of the symbol is the divisor dividend xor, the remainder symbol is the dividend symbol), So the array becomes negative, for ease of handling. Are dialectical. But what about the final sign? As a matter of fact, the array processing, 3m 1,3n 1 another 0/1,
Add up the mold to take the representation of the symbol bit 0 1 out.




However, from the error point of view, another -2^31 error, and then think about it, the sign bit changed 1, and then 10000 1+31 0 results that 1 can not be installed, so his complement is 10000000, so special treatment.
The lower level is achieved here. Achieve the complement.




Handle the logic after the commit. It's finally over T T.


Time complexity O (32n) =o (n), Spatial complexity O (1)

PS: The code before the code that directly copy the Circle God:)

#include <map> #include <set> #include <queue> #include <stack> #include <math.h> #include <time.h> #include <stdio.h> #include <stdlib.h> #include <iostream> #include <limits.h># Include <string.h> #include <string> #include <algorithm> #include <sstream> #include < Iomanip> #define Min (b) ((a) < (a))?

(a): (b) #define MAX (a) ((a) > (b))? (a): (b) #define Read freopen ("In.txt", "R", stdin) #define Write Freopen ("OUT.txt", "w", stdout) using namespace std;//# Define Maxbitnum 32//#define Maxnum 100000//int bitnumvec[maxnum][maxbitnum];int singlenumber (int a[], int n) {//vector& Lt;int*> vec;if (n==1) return a[0];const int maxbitnum=32; int bitnumvec[maxnum][maxbitnum];int** bitnumvec=new int*[n];for (int i=0;i<n;i++) bitnumvec[i]=new int[ Maxbitnum] (); for (int i=0;i<n;i++) {int offset=maxbitnum-1;if (A[I]==-POW (2.0,31))//-2^31{bitnumvec[i][0]=1;//, 10000000...000 }else//others{if (A[i]<0&&a[i]>-pow (2.0,31))//NEGATIVE{BITNUMVEC[I][0]=1;//1 means negative, 0 means Positvea[i]=-a[i];} while (a[i]!=0) {bitnumvec[i][offset]=a[i]%2;//bitnum[offset]=a[i]%2; a[i]=a[i]/2;offset--;}} Reverse (Vec.begin (), Vec.end ());//vec.push_back (bitnum);} memset (bitnum,0,sizeof (int) *maxbitnum); int Bitnum[maxbitnum];memset (bitnum,0,sizeof (int) *maxbitnum); int x=0; for (int I=0;i<maxbitnum;i++) {//if (i==maxbitnum-1)//int y=1;for (int j=0;j<n;j++) {//if (bitnumvec[j][0]==0) bitnum[i]+=bitnumvec[j ][i];//else if (bitnumvec[j][0]==1)//bitnum[i]-=bitnumvec[j][i];} Bitnum[i]=bitnum[i]%3;if (i>0) X+=bitnum[i]*pow (2.0,maxbitnum-1-i);} if (bitnum[0]==1 &&x!=0) x=-x;else if (bitnum[0]==1 && x==0) X=-pow (2.0,31);//for (int i=0;i< maxbitnum;i++)//int x;//for (int i=0;i<maxbitnum;i++) for (int i=0;i<n;i++) delete[] bitnumvec[i];d elete[] Bitnumvec;return x;} int main () {//int x=-3%2;int a[]={-2,-2,-2147483648,-2};cout<<singlenumber (a,4) <<endl;return 0;}



Find the only number O (n) bit arithmetic that appears 1 times without appearing three times

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.