【LeetCode】982. Triples with Bitwise AND Equal To Zero 解题报告(C++)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/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.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 <= A.length <= 1000
- 0 <= A[i] < 2^16
题目大意
找出给定的数组中,有多少个三元组,使得这个三元组的位与等于0.
解题方法
看了下数组长度是1000,大概分析下这个题目的时间复杂度是O(N^2×log(N))以下。同时注意到三元组的顺序并无要求,即同样的组合可能出现多次。
一个最省事的做法就是,先两重循环,计算任意两个数字之间的位与结果是多少,存储到字典中,字典中保存的是位与出现的次数。然后再次对数组每个位置进行遍历,同时遍历字典中的每个元素,即可分析出任意三个数字位与的结果和次数。
唯一需要注意的是字典保存的是两个数字位与后的结果出现的次数,当第三个数字和两位数字位与结果进行位与的结果是0的时候,需要次数累加。
这个时间复杂度怎么分析?注意题目给出的每个元素的大小是216,所以两个数字位与的结果不会超过216。因此,总的时间复杂度是O(N^2 + 2^16 * N).
class Solution {
public:
int countTriplets(vector<int>& A) {
const int N = A.size();
int res = 0;
unordered_map<int, int> m_;
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
++m_[(A[i] & A[j])];
}
}
for (int i = 0; i < N; ++i) {
for (auto a : m_) {
if ((A[i] & a.first) == 0)
res += a.second;
}
}
return res;
}
};
日期
2019 年 1 月 27 日 —— 这个周赛不太爽
【LeetCode】982. Triples with Bitwise AND Equal To Zero 解题报告(C++)的更多相关文章
- LeetCode 982. Triples with Bitwise AND Equal To Zero
题目链接:https://leetcode.com/problems/triples-with-bitwise-and-equal-to-zero/ 题意,已知数组A,长度不超过1000,最大的数不超 ...
- 【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 < ...
- 【LeetCode】453. Minimum Moves to Equal Array Elements 解题报告(Java & Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:模拟过程 方法二:求和-n*最小值 方法三: ...
- 【LeetCode】698. Partition to K Equal Sum Subsets 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...
- 【LeetCode】201. Bitwise AND of Numbers Range 解题报告(Python)
[LeetCode]201. Bitwise AND of Numbers Range 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/prob ...
- 【LeetCode】375. Guess Number Higher or Lower II 解题报告(Python)
[LeetCode]375. Guess Number Higher or Lower II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- 【LeetCode】430. Flatten a Multilevel Doubly Linked List 解题报告(Python)
[LeetCode]430. Flatten a Multilevel Doubly Linked List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: ...
- 【LeetCode】153. Find Minimum in Rotated Sorted Array 解题报告(Python)
[LeetCode]153. Find Minimum in Rotated Sorted Array 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode. ...
- 【LeetCode】373. Find K Pairs with Smallest Sums 解题报告(Python)
[LeetCode]373. Find K Pairs with Smallest Sums 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/p ...
随机推荐
- Windows下的Python多版本管理?
虽然接触了好几年python,但一些细节没有注意.最近看网课,看到这个Windows系统下Python多版本管理的问题,记录下备忘. 假设现在windows环境中有python2,python3和an ...
- Docker Alpine Dockerfile 安装nginx,最小镜像
Docker Alpine Dockerfile 安装nginx,最小镜像 FROM alpine MAINTAINER will ## 将alpine-linux:apk的安装源改为国内镜像 RUN ...
- CSS浮动效果
#div1{ background-color: yellow; width: 150px; height:150px; position: absolute; top:150px; left: 15 ...
- linux 两服务器之间的文件传输scp
Linux scp 命令用于 Linux 之间复制文件和目录. scp 是 secure copy 的缩写, scp 是 linux 系统下基于 ssh 登陆进行安全的远程文件拷贝命令. scp 是加 ...
- 模拟串口UART的实现
我所祷告的,就是要你们的爱心,在知识和见识上,多而又多,使你们能分辨是非,做诚实无过的人,直到基督的日子.--腓立比书[1:9~10] 最近在调的MCU的型号为STM32F030,配置芯片相较之前的M ...
- MySQL压力测试工具
一.MySQL自带的压力测试工具--Mysqlslap mysqlslap是mysql自带的基准测试工具,该工具查询数据,语法简单,灵活容易使用.该工具可以模拟多个客户端同时并发的向服务器发出查询更新 ...
- MyBatis中sql实现时间查询的方法
<if test="startTime != null and startTime !=''"> AND lTime >= #{startTime} </i ...
- File类及常用操作方法
import java.io.File; import java.io.IOException; public class file { public static void main(String[ ...
- Java Criteria使用方法
Criteria Query 可以看作传统sql的对象化表示. Criteria 可以由session创建. Criteria ct= session.createCriteria(TUser.cla ...
- lucene的索引查询
package com.hope.lucene;import org.apache.lucene.document.Document;import org.apache.lucene.document ...