代码拉取完成,页面将自动刷新
# Time: O(nlogn), n is the max of A
# Space: O(n)
import collections
# Reference: https://blog.csdn.net/john123741/article/details/76576925
# FWT solution
class Solution(object):
def countTriplets(self, A):
"""
:type A: List[int]
:rtype: int
"""
def FWT(A, v):
B = A[:]
d = 1
while d < len(B):
for i in xrange(0, len(B), d << 1):
for j in xrange(d):
B[i+j] += B[i+j+d] * v
d <<= 1
return B
k = 3
n, max_A = 1, max(A)
while n <= max_A:
n *= 2
count = collections.Counter(A)
B = [count[i] for i in xrange(n)]
C = FWT(map(lambda x : x**k, FWT(B, 1)), -1)
return C[0]
# Time: O(n^3), n is the length of A
# Space: O(n^2)
import collections
class Solution2(object):
def countTriplets(self, A):
"""
:type A: List[int]
:rtype: int
"""
count = collections.defaultdict(int)
for i in xrange(len(A)):
for j in xrange(len(A)):
count[A[i]&A[j]] += 1
result = 0
for k in xrange(len(A)):
for v in count:
if A[k]&v == 0:
result += count[v]
return result
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。