【发布时间】:2019-02-14 05:33:40
【问题描述】:
给定一个大小为 N 的列表。找到满足 A[i] XOR A[j] = x 且 1
输入:list = [3, 6, 8, 10, 15, 50], x = 5
输出:2
解释:(3 ^ 6) = 5 和 (10 ^ 15) = 5
这是我的代码(蛮力):
import itertools
n=int(input())
pairs=0
l=list(map(int,raw_input().split()))
q=[x for x in l if x%2==0]
p=[y for y in l if y%2!=0]
for a, b in itertools.combinations(q, 2):
if (a^b!=2) and ((a^b)%2==0) and (a!=b):
pairs+=1
for a, b in itertools.combinations(p, 2):
if (a^b!=2) and ((a^b)%2==0) and (a!=b):
pairs+=1
print pairs
如何在 Python 中 O(n) 的复杂性中更有效地做到这一点?
【问题讨论】: