题目链接:https://leetcode.com/problems/triples-with-bitwise-and-equal-to-zero/

题意,已知数组A,长度不超过1000,最大的数不超过$2^{16}$,问存在多少对i,j,k,使得A[i]^A[j]^a[k]=0,i,j,k可以相等。

若直接暴力复杂度为$O(n^3)$,观察到数组不超过$2^{16}$,因此可以先枚举i和j,将结果存储,则结果不超过$2^{17}$,再查找符合的k,复杂度$O(n^2+n*2^{17})$。

class Solution {
public:
int countTriplets(vector<int>& A) {
int ans=0;
vector<int> mp(1<<16,0); //存储A[i]*A[j]的个数
for(int i=0;i<A.size();i++) //枚举i和j
for(int j=0;j<A.size();j++)
mp[A[i]&A[j]]++;
for(int i=0;i<A.size();i++) //枚举k
for(auto j = 0 ;j<(1<<16); j++){
if((A[i]&j)==0)
ans+=mp[j];
else j=j+(A[i]&j)-1;
}
return ans;
}
};

  

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

  1. 【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 < ...

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

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

  3. [Swift]LeetCode982. 按位与为零的三元组 | 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 & ...

  4. leetcode982 Triples with Bitwise AND Equal To Zero

    思路: 使用unordered_map暴力枚举. 实现: #include <bits/stdc++.h> using namespace std; class Solution { pu ...

  5. 【LeetCode】453. Minimum Moves to Equal Array Elements 解题报告(Java & Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:模拟过程 方法二:求和-n*最小值 方法三: ...

  6. 【LeetCode】462. Minimum Moves to Equal Array Elements II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:排序 方法二:直接找中位数 日期 题目地址: ...

  7. 【LeetCode】698. Partition to K Equal Sum Subsets 解题报告(Python & C++)

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

  8. 【LeetCode】462. Minimum Moves to Equal Array Elements II

    Given a non-empty integer array, find the minimum number of moves required to make all array element ...

  9. LeetCode算法题-Minimum Moves to Equal Array Elements(Java实现)

    这是悦乐书的第233次更新,第246篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第100题(顺位题号是453).给定大小为n的非空整数数组,找到使所有数组元素相等所需的 ...

随机推荐

  1. Java 将PPT幻灯片转为HTML

    本文以Java程序代码为例展示如何通过格式转换的方式将PPT幻灯片文档转为HTML文件.这里的PPT幻灯片可以是.ppt/.pptx/.pps/.ppsx/.potx等格式. 代码实现思路:[加载PP ...

  2. 并发王者课-铂金1:探本溯源-为何说Lock接口是Java中锁的基础

    欢迎来到<并发王者课>,本文是该系列文章中的第14篇. 在黄金系列中,我们介绍了并发中一些问题,比如死锁.活锁.线程饥饿等问题.在并发编程中,这些问题无疑都是需要解决的.所以,在铂金系列文 ...

  3. string大小写转换

    string大小写转换 源码: 1 #include <string> 2 #include <iostream> 3 #include <algorithm> 4 ...

  4. go语言的排序和搜索(转载)

    http://studygolang.com/articles/1598 go语言的排序和搜索 晚上准备动手写点 go 的程序的时候,想起 go 如何排序的问题.排序 sort 是个基本的操作,当然搜 ...

  5. 关于web移动端定位

    最近在做一个搜索附近3公里所有超市信息(已经录入数据库的超市信息)的功能.思路很简单只是获取用户当前地理位置(经纬度),通过sql语句筛选出3公里范围内的所有超市信息,然后传递到前台页面展示出来.但是 ...

  6. 【题解】codeforces 467C George and Job dp

    题目描述 新款手机 iTone6 近期上市,George 很想买一只.不幸地,George 没有足够的钱,所以 George 打算当一名程序猿去打工.现在George遇到了一个问题. 给出一组有 n ...

  7. Linux shell是什么

    shell概念: shell是一个命令行解释器,它为用户提供了一个向Linux内核发送请求以便运行程序的界面系统级程序,用户可以用shell启动,挂起,停止甚至编写一些程序. shell还是一个功能强 ...

  8. 『心善渊』Selenium3.0基础 — 4、Selenium基础元素定位详解

    目录 1.什么是元素定位 2.Selenium元素定位常用API (1)By_id 定位 (2)by_name 定位 (3)by_class_name 定位 (4)by_tag_name 定位 (5) ...

  9. VBS脚本编程(10)——编写WMI脚本

    WMI介绍 1.WMI是什么? WMI--Windows管理规范(Windows Management instrumentation). 是一项核心的Windows管理技术. 采用统一的.基于开放标 ...

  10. centos7安装kubernetes1.18.5

    一.设置hosts 修改主机名 [root@localhost kubernetes]# hostnamectl set-hostname master69 四台服务器安装kebernetes,一个m ...