Ai
1 Star 2 Fork 5

LilithSangreal/LeetCode-Solutions

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
triples-with-bitwise-and-equal-to-zero.py 1.30 KB
一键复制 编辑 原始数据 按行查看 历史
kamyu 提交于 2019-01-27 20:52 +08:00 . Update triples-with-bitwise-and-equal-to-zero.py
# 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
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/LilithSangreal/LeetCode-Solutions.git
git@gitee.com:LilithSangreal/LeetCode-Solutions.git
LilithSangreal
LeetCode-Solutions
LeetCode-Solutions
master

搜索帮助