直接跳转:https://blog.youkuaiyun.com/Allenlzcoder/article/details/134566485
Solution1:
书中展示了很棒的思路:
关于异或的两个性质应该知道:
(1) X^X = 0;
(2) X^0 = X;
class Solution:
def sockCollocation(self, sockets: List[int]) -> List[int]:
x = y = n = 0
tag = 1
for item in sockets:
n ^= item
while n & tag == 0:
tag <<= 1
for item in sockets:
if item & tag == 0:
x ^= item
else:
y ^= item
return [x, y]
剑指offer:数组中只出现一次的数字
本文介绍了一种高效算法,通过异或运算找出数组中仅出现一次的两个数。利用异或的特性,先对所有元素进行异或得到一个结果,再通过查找该结果中第一个为1的比特位将数组分为两组,分别对这两组进行异或运算即可求得那两个唯一的数。
1230

被折叠的 条评论
为什么被折叠?



