LeetCode 260. Single Number III

题目

Given an array of numbers nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once.

Example:

1
2
Input:  [1,2,1,3,2,5]
Output: [3,5]

Note:

  1. The order of the result is not important. So in the above example, [5, 3] is also correct.
  2. Your algorithm should run in linear runtime complexity. Could you implement it using only constant space complexity?

思路

首先计算nums数组中所有数字的异或,记为xor

令lowbit = xor & -xor,lowbit的含义为xor从低位向高位,第一个非0位所对应的数字

例如假设xor = 6(二进制:0110),则-xor为(二进制:1010,-6的补码,two’s complement)

则lowbit = 2(二进制:0010)

根据异或运算的性质,“同0异1”

记只出现一次的两个数字分别为a与b

可知a & lowbit与b & lowbit的结果一定不同

通过这种方式,即可将a与b拆分开来

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution(object):
def singleNumber(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
xor = reduce(lambda x, y : x ^ y, nums)
lowbit = xor & -xor
a = b = 0
for num in nums:
if num & lowbit:
a ^= num
else:
b ^= num
return [a, b]