【leetcode】982. Triples with Bitwise AND Equal To Zero
发布时间:2020-12-14 04:25:39 所属栏目:大数据 来源:网络整理
导读:题目如下: Given an array of integers? A ,find the number of?triples of indices (i,j,k)?such that: 0 = i A.length 0 = j A.length 0 = k A.length A[i]? A[j]? A[k] == 0 ,where? ?represents the bitwise-AND operator. ? Example 1: Input: [2,1,3]
题目如下:
解题思路:我的方法和 3Sum 题一样,就是先算出A中任意两个数的与值,然后再和A中所有值与操作判断是否为0,耗时3秒多。不管怎么样,至少通过了。
代码如下: class Solution(object): def countTriplets(self,A): """ :type A: List[int] :rtype: int """ dic = {} for i in A: for j in A: v = i & j dic[v] = dic.setdefault(v,0) + 1 res = 0 for i in A: for k,v in dic.iteritems(): if i & k == 0: res += v return res (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |