面试题56 - I
一个整型数组 nums 里除两个数字之外,其他数字都出现了两次。请写程序找出这两个只出现一次的数字。要求时间复杂度是O(n),空间复杂度是O(1)。
限制:
2 <= nums <= 10000
Solutions
bit operation
Use xor to remove numbers occurred two times and the resulting xored number
res
isnum1 ^ num2
.Then partition the whole array into two parts containing
num1
andnum2
in each part.Randomly choose one
1
bit inres
as the partition criteria.
Repeat the first step for each part.
Last updated
Was this helpful?