题目如下:

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]
Output: 12
Explanation: We could choose the following i, j, k triples:
(i=0, j=0, k=1) : 2 & 2 & 1
(i=0, j=1, k=0) : 2 & 1 & 2
(i=0, j=1, k=1) : 2 & 1 & 1
(i=0, j=1, k=2) : 2 & 1 & 3
(i=0, j=2, k=1) : 2 & 3 & 1
(i=1, j=0, k=0) : 1 & 2 & 2
(i=1, j=0, k=1) : 1 & 2 & 1
(i=1, j=0, k=2) : 1 & 2 & 3
(i=1, j=1, k=0) : 1 & 1 & 2
(i=1, j=2, k=0) : 1 & 3 & 2
(i=2, j=0, k=1) : 3 & 2 & 1
(i=2, j=1, k=0) : 3 & 1 & 2

Note:

  1. 1 <= A.length <= 1000
  2. 0 <= A[i] < 2^16

解题思路:我的方法和 3Sum 题一样,就是先算出A中任意两个数的与值,然后再和A中所有值与操作判断是否为0,耗时3秒多。不管怎么样,至少通过了。

Runtime: 3772 ms, faster than 39.02% of Python online submissions for Triples with Bitwise AND Equal To Zero.

代码如下:

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

【leetcode】982. Triples with Bitwise AND Equal To Zero的更多相关文章

  1. 【LeetCode】982. Triples with Bitwise AND Equal To Zero 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  2. LeetCode 982. Triples with Bitwise AND Equal To Zero

    题目链接:https://leetcode.com/problems/triples-with-bitwise-and-equal-to-zero/ 题意,已知数组A,长度不超过1000,最大的数不超 ...

  3. 【LeetCode】1007. Minimum Domino Rotations For Equal Row 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历一遍 日期 题目地址:https://leetc ...

  4. 【leetcode】1007. Minimum Domino Rotations For Equal Row

    题目如下: In a row of dominoes, A[i] and B[i] represent the top and bottom halves of the i-th domino.  ( ...

  5. 【LeetCode】201. Bitwise AND of Numbers Range 解题报告(Python)

    [LeetCode]201. Bitwise AND of Numbers Range 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/prob ...

  6. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  7. 【Leetcode】Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  8. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

  9. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

随机推荐

  1. Spring Transaction Propagation

    原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11530611.html REQUIRED behavior Spring REQUIRED behav ...

  2. 阿里云数据库备份DBS商业化发布,数据库实时备份到OSS

    数据库备份DBS已于2018年5月17日正式商业化发布. 数据库备份(Database Backup,简称DBS)是为数据库提供连续数据保护.低成本的备份服务. 它可以为多种环境的数据提供强有力的保护 ...

  3. sqlserver2008锁表语句详解(锁定数据库一个表)

    锁定数据库的一个表 复制代码代码如下: SELECT * FROM table WITH (HOLDLOCK) 注意: 锁定数据库的一个表的区别 复制代码代码如下: SELECT * FROM tab ...

  4. ldd3 编写scull尝试

    快速参考: #include <linux/types.h> dev_t dev_t is the type used to represent device numbers within ...

  5. codeforces 582A GCD Table

    题意简述: 给定一个长度为$n$的序列 将这个序列里的数两两求$gcd$得到$n^2$个数 将这$n^2$个数打乱顺序给出 求原序列的一种可能的情况 ------------------------- ...

  6. 用DECODE进行排序

    DECODE用法: 现定义一table名为output,其中定义两个column分别为monthid(var型)和sale(number型),若sale值=1000时翻译为D,=2000时翻译为C,= ...

  7. 安装Elasticsearch5.4.0以及head,kibana插件

    可以在网盘中下载也可以去官网下载 网盘: Elasticsearch 地址:http://pan.baidu.com/s/1hrI0AFU elasticsearch-head  地址:http:// ...

  8. linux shell assemble PDF文件

      daniel@daniel-mint ~/latex/linux/itext/daniel $ cat asm.sh header_start=0 header_len=15 xref_start ...

  9. 嵌入式C语言3.4 关键字---类型描述符auto/register/static/const/extern/volatile/

    对内存资源存放位置的限定 1. auto 默认值---分配的内存都是可读可写的区域 auto int a; 区域如果出现 {} 我们认为在栈空间 2. register register int a; ...

  10. java中四种访问修饰符区别及详解全过程

    客户端程序员:即在其应用中使用数据类型的类消费者,他的目标是收集各种用来实现快速应用开发的类. 类创建者:即创建新数据类型的程序员,目标是构建类. 访问控制存在的原因:a.让客户端程序员无法触及他们不 ...