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?
public class Solution { public int singleNumber(int[] A) { int ones = 0; int twos = 0; int not_threes=0, x=0; for (int i=0; i<A.length; ++i) { x = A[i]; twos |= ones & x; ones ^= x; not_threes = ~(ones & twos); ones &= not_threes; twos &= not_threes; } return twos!=0?twos:ones; } }