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