题目如下:

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

最新文章

  1. jQuery LigerUI系列:ligerComboBox
  2. Python标准模块--multiprocessing
  3. Swift-Switch穿透
  4. mysql多实例(个人的情况,不是大众的)里面有配置好的脚本+主从复制
  5. 【转】轻松搞定FTP之FlashFxp全攻略
  6. (转)ZooKeeper 笔记(1) 安装部署及hello world
  7. Android自定义UI的实现和应用
  8. 【软测试】(两)计算机组成原理-cpu
  9. Zookeeper与Kafka基础概念和原理
  10. 《CSS世界》读书笔记(十五)
  11. 论文阅读笔记十八:ENet: A Deep Neural Network Architecture for Real-Time Semantic Segmentation(CVPR2016)
  12. too much recursion(太多递归)Uncaught RangeError: Maximum call stack size exceeded BootstrapValidator报错
  13. 【模板】倍增LCA
  14. MYSQL + MHA +keepalive + VIP安装配置(二)--MHA的配置
  15. .NET MVC+ EF+调用存储过程 多表联查以及VIEW列表显示
  16. WebLogic 任意文件上传 远程代码执行漏洞 (CVE-2018-2894)-------&gt;&gt;&gt;任意文件上传检测POC
  17. RDLC报表学习
  18. c++拷贝构造函数,深拷贝,浅拷贝,对象内存
  19. DateTime.Now.ToString(&quot;yyyy/MM/dd&quot;) 时间格式化中的MM为什么是大写的?
  20. Gradient Boosting算法简介

热门文章

  1. ci常量
  2. Python基础教程(022)--Pycharm快速体验
  3. C# 后台报错输出到日志
  4. Java学习之多线程(定义)
  5. java方法调用及传参
  6. selenium和phantomjs,完成豆瓣音乐排行榜的内容爬取
  7. 一个spark SQL和DataFrames的故事
  8. ZR-19CSP-S赛前冲刺
  9. arcpy脚本使用多接图表图斑对对应多幅影像进行裁边处理
  10. 下载Spring各个版本的jar包