Single Number II
Given an array of integers, every element appears three times except for one. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
class Solution { public: int singleNumber(int A[], int n) { // Note: The Solution object is instantiated only once and is reused by each test case. if(n==1){ return A[0]; } if(n%3!=1){ return -1; } const size_t int_len=8*sizeof(int); int tmp[int_len]; size_t i; for(i=0;i<int_len;i++){ tmp[i]=0; } int index; for(index=0;index<n;index++){ for(i=0;i<int_len;i++){ tmp[i]+=(A[index]>>i)&(1); } } int res=0; for(i=0;i<int_len;i++){ if(tmp[i]%3){ res+=(1)<<(i); } } return res; } };