当前位置:网站首页>leetcode-6127:优质数对的数目
leetcode-6127:优质数对的数目
2022-07-25 20:36:00 【菊头蝙蝠】
题目
题目连接
给你一个下标从 0 开始的正整数数组 nums 和一个正整数 k 。
如果满足下述条件,则数对 (num1, num2) 是 优质数对 :
- num1 和 num2 都 在数组 nums 中存在。
- num1 OR num2 和 num1 AND num2 的二进制表示中值为 1 的位数之和大于等于 k ,其中 OR 是按位 或 操作,而 AND 是按位 与 操作。
返回 不同 优质数对的数目。
如果 a != c 或者 b != d ,则认为 (a, b) 和 (c, d) 是不同的两个数对。例如,(1, 2) 和 (2, 1) 不同。
注意:如果 num1 在数组中至少出现 一次 ,则满足 num1 == num2 的数对 (num1, num2) 也可以是优质数对。
示例 1:
输入:nums = [1,2,3,1], k = 3
输出:5
解释:有如下几个优质数对:
- (3, 3):(3 AND 3) 和 (3 OR 3) 的二进制表示都等于 (11) 。值为 1 的位数和等于 2 + 2 = 4 ,大于等于 k = 3 。
- (2, 3) 和 (3, 2): (2 AND 3) 的二进制表示等于 (10) ,(2 OR 3) 的二进制表示等于 (11) 。值为 1 的位数和等于 1 + 2 = 3 。
- (1, 3) 和 (3, 1): (1 AND 3) 的二进制表示等于 (01) ,(1 OR 3) 的二进制表示等于 (11) 。值为 1 的位数和等于 1 + 2 = 3 。
所以优质数对的数目是 5 。
示例 2:
输入:nums = [5,1,1], k = 10
输出:0
解释:该数组中不存在优质数对。

解题
方法一:等价转换
对于 x|y 和x&y中的 1,在同一个比特位上,如果都有 1,那这个 1 会被统计两次;如果一个为 1 另一个为 0,那这个 1 会被统计一次。
因此可以等价替换,与、或结果 的二进制位1的个数 和,就是两个数分别二进制位1的个数和。
class Solution {
public:
int getBits(int num){
int res=0;
while(num){
res+=num&1;
num>>=1;
}
return res;
}
long long countExcellentPairs(vector<int>& nums, int k) {
//去重
sort(nums.begin(),nums.end());
nums.resize(unique(nums.begin(),nums.end())-nums.begin());
// nums.erase(unique(nums.begin(),nums.end()),nums.end()); //这两种都可以
int n=nums.size();
vector<int> cnt(64,0);//bits个数 ---->数量
for(int i=0;i<n;i++){
int bits=getBits(nums[i]);//求位1的数量
cnt[bits]++;
}
long long res=0;
for(int i=1;i<=60;i++){
for(int j=1;j<=60;j++){
if(i+j>=k){
res+=1ll*cnt[i]*cnt[j];
}
}
}
return res;
}
};
边栏推荐
- DIY个人服务器(diy存储服务器)
- 2022.7.24-----leetcode.1184
- Cloud native guide: what is cloud native infrastructure
- Chapter VI modified specification (SPEC) class
- Increase swap space
- 【高等数学】【4】不定积分
- 从底层结构开始学习FPGA(16)----PLL/MMCM IP的定制与测试
- FormatDateTime说解[通俗易懂]
- [matlab] download originality documents based on oil monkey script and MATLAB
- 网络协议:TCP Part2
猜你喜欢

During the interview, I was asked how to remove the weight of MySQL, and who else wouldn't?
![[advanced drawing of single cell] 07. Display of KEGG enrichment results](/img/60/09c5f44d64b96c6e4d57e5f426e4ed.png)
[advanced drawing of single cell] 07. Display of KEGG enrichment results
![[today in history] July 5: the mother of Google was born; Two Turing Award pioneers born on the same day](/img/7d/7a01c8c6923077d6c201bf1ae02c8c.png)
[today in history] July 5: the mother of Google was born; Two Turing Award pioneers born on the same day

Online random coin tossing positive and negative statistical tool

103. (cesium chapter) cesium honeycomb diagram (square)

Network protocol: TCP part2

Kubernetes进阶部分学习笔记

How to choose a microservice registration center?

【TensorRT】动态batch进行推理

LeetCode通关:哈希表六连,这个还真有点简单
随机推荐
Principle analysis of bootloader
DIY personal server (DIY storage server)
网络RTK无人机上机测试[通俗易懂]
KEGG通路的从属/注释信息如何获取
Compilation and operation of program
Timing analysis and constraints based on xlinx (1) -- what is timing analysis? What are temporal constraints? What is temporal convergence?
C language file reading and writing
4everland storage node portal network design
从底层结构开始学习FPGA(16)----PLL/MMCM IP的定制与测试
Arrow parquet
Clickhouse notes 02 -- installation test clickvisual
FormatDateTime说解[通俗易懂]
RF, gbdt, xgboost feature selection methods "recommended collection"
How to obtain the subordinate / annotation information of KEGG channel
【TensorRT】动态batch进行推理
【ONNX】pytorch模型导出成ONNX格式:支持多参数与动态输入
【高等数学】【6】多元函数微分学
Cloud native, Intel arch and cloud native secret computing three sig online sharing! See you today | issues 32-34
During the interview, I was asked how to remove the weight of MySQL, and who else wouldn't?
LeetCode通关:哈希表六连,这个还真有点简单