To solve this in O(1) space, we can use Bit Manipulation to count the number of 1-bits at each position (0-31). Since every number appears 3 times except for one, the sum of bits at each position modulo 3 will reveal the bits of the single number.
- Initialize
ans = 0. - Iterate through
ifrom 0 to 31:count = 0.- For every
numinnums:- If the
i-th bit ofnumis 1,count += 1.
- If the
count %= 3.- If
count == 1, set thei-th bit ofans.
- Handle negative numbers (Python integers are infinite, so bits are tricky).
- Time Complexity: O(32 * N) = O(N).
- Space Complexity: O(1).
def single_number(nums):
ones, twos = 0, 0
for n in nums:
ones = (ones ^ n) & (~twos)
twos = (twos ^ n) & (~ones)
return ones(Note: The ones/twos approach is a more elegant bitwise state-machine version of the bit-counting logic).