260. Single Number III
- Lowest Common Ancestor of Deepest Leaves python solution
题目描述
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.
解析
给出两种方法进行求解。第一种是简单粗暴的,见代码
class Solution(object):
def singleNumber(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
one = set()
double = set()
for n in nums:
if n not in one:
one.add(n)
else:
double.add(n)
return list(one - double)
第二种是使用异或操作的,需要讲解一下基础知识。
A xor A=0
0 xor A=A
并且 xor 满足交换律和结合律。
比如 2 2 3 1 3,2 xor 2 xor 3 xor 1 xor 3
= 2 xor 2 xor 3 xor 3 xor 1
=(2 xor 2) xor (3 xor 3) xor 1
= 0 xor 0 xor 1
= 1
如果将所有的数字都xor 一遍,那结果是什么呢。假设只出现一次的两个数是 A、B,那我们最后只能得到一个值 = A xor B,但没有办法知道 A 是多少,B 是多少。
xor 是按位比较,相同为0,不同为1,也就是说得到的这个值里,所有的1都代表了:在这个位置上,A 和 B 是不同的,这给我们区分 A B 提供了一个方法:
我们随便找一个是1的位置(也就是 A和B 在这个位置上的值反正有一个是0 有一个是1),再次遍历一遍数组,按照这个位置上是0还是1分成两组,那么 A 和 B 一定会被分开。而对于其他的数字,无论他们在这个位置上是0还是1,总之他们会两两一对分到两个组中的任意一个组里去。
这就转化成了初级版本的问题了,每个组中都只有一个数出现1次,对两个组分别做一次xor ,得到两个数就是 A 和 B。
class Solution(object):
def singleNumber(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
xor = 0
a = 0
b = 0
for num in nums:
xor ^= num
mask = 1
while(xor&mask == 0):
mask = mask << 1
for num in nums:
if num&mask:
a ^= num
else:
b ^= num
return [a, b]
Reference
https://leetcode.com/problems/single-number-iii/discuss/68907/Naive-Python
https://leetcode.com/problems/single-number-iii/discuss/68931/Easy-Python-O(n)-O(1)-solution