Problems:
Given 3*n + 1 numbers, every numbers occurs triple times except one, find it.
Example
Given [1,1,2,3,3,3,2,2,4,1] return 4
Challenge
One-pass, constant extra space.
Solution:
This problem examines the ability of applying bit operation.
First, we create an integer array with size of 32, each int represent a bit of an integer.
Then for each bit position of an integer, we go through the input numbers, and accululate the value of the correpsonding bit.
As we know, the sum of each bit shold be dividable by 3 except one number.
So we mod 3 for each bit, then the left number is the result.
1 | /************************************************************************* |