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 []nums)
{
int ones = 0;
int twos = 0;
int threes = 0;
for(int num : nums)
{
twos = twos | (num&ones);
ones = ones ^ num;
threes = ones & twos;
ones = ones & (~threes);
twos = twos & (~threes);
}
int res = ones | twos;
return res;
}
}