Given an array of integers, every element appears three times except for one. Find the single one.
Note:
Your algorithm should has a linear runtime complexity. Could you implement it without using extra memory?
Ideas:
STEP1: From single number we know that through easy or operation can be stored in an int for a few times
STEP2: We can get the number of double-digit occurrences by doing with the singular.
STEP3: Try to get rid of the STEP1 int three times.
classSolution { Public: intSinglenumber (intA[],intN) {intonce =0; inttwice =0; for(inti =0; I < n; i++) {twice|= once & A[i];//The num appeared 2 timesOnce ^= a[i];//The num appeared 1 times intNot_three = ~ (once & twice);//The num not appeared 3 timesOnce = Not_three & once;//Remove num appeared 3 times from oncetwice = Not_three & twice;//Remove num appeared 3 times from twice } returnonce; }};
137. Single number II (Bit)