【leetcode】982. Triples with Bitwise AND Equal To Zero
题目如下:
Given an array of integers
A, find the number of triples of indices (i, j, k) such that:
0 <= i < A.length0 <= j < A.length0 <= k < A.lengthA[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 <= 10000 <= 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的更多相关文章
- 【LeetCode】982. Triples with Bitwise AND Equal To Zero 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- LeetCode 982. Triples with Bitwise AND Equal To Zero
题目链接:https://leetcode.com/problems/triples-with-bitwise-and-equal-to-zero/ 题意,已知数组A,长度不超过1000,最大的数不超 ...
- 【LeetCode】1007. Minimum Domino Rotations For Equal Row 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历一遍 日期 题目地址:https://leetc ...
- 【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. ( ...
- 【LeetCode】201. Bitwise AND of Numbers Range 解题报告(Python)
[LeetCode]201. Bitwise AND of Numbers Range 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/prob ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
随机推荐
- html中 的method=post和method=get的区别
1. get是从服务器上获取数据,post是向服务器传送数据. 2. get是把参数数据队列加到提交表单的ACTION属性所指的URL中,值和表单内各个字段一一对应,在URL中可以看到.post是通过 ...
- vuejs如何调试代码
基于webpack的配置调试 使用Vue-cli命令行工具初始化基于wabpack模板的项目的命令语法: npm install -g @vue/cli # 全局安装vue-cli,版本vue3.x ...
- 51nod 1836:战忽局的手段(期望)
题目链接 公式比较好推 精度好难搞啊@_@ 下面记笔记@_@ ****在CodeBlocks中,输出double型变量要使用%f (参见http://bbs.csdn.net/topics/39193 ...
- linux学习-用户组与权限管理
一.用户与组 1.用户 管理员:root,UID为0 普通用户:1-60000 自动分配 系统用户:1-499,1-999(Centos7),对守护进程分配获取资源进行权限分配 登录用户:500+,1 ...
- python每日练习10题
161.求1000以内的所有的素数以及闰年的数之和 第一步:求1000以内的素数,素数:只能被1和本身整除的数叫素数 import math def is_prime(num): if num ==1 ...
- Python基础教程(020)--集成开发环境IDE简介--Pycharm
前言 学会掌握Pycharm工具 内容 集成了开发软件需要的所有工具 1,图形用户界面 2,代码编译器(支持代码补全,自动缩进) 3,编译器,解释器 4,调试器(断点,单步执行) Pycharm介绍 ...
- BZOJ 5296: [Cqoi2018]破解D-H协议(BSGS)
传送门 解题思路 \(BSGS\)裸题??要求的是\(g^a =A (mod\) \(p)\),设\(m\)为\(\sqrt p\),那么可以设\(a=i*m-j\),式子变成 \[ g^{i*m-j ...
- 2018-2019-2 网络对抗技术 20165206 Exp 8 Web基础
- 2018-2019-2 网络对抗技术 20165206 Exp 8 Web基础 - 实验任务 (1).Web前端HTML(0.5分) 能正常安装.启停Apache.理解HTML,理解表单,理解GE ...
- git filter-branch
https://github.com/git-for-windows/git/issues/2206 https://git-scm.com/docs/git-filter-branch The -- ...
- sudo su 和sudo -s的区别
sudo su 和 sudo -s都是切换到root用户,不同的是: sudo su 环境用的是目标用户(root)的环境 sudo -s 环境用的是当前用户本身的环境