当前位置:网站首页>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;
}
};
边栏推荐
- Cloud native guide: what is cloud native infrastructure
- Do you still have certificates to participate in the open source community?
- Apache MINA框架「建议收藏」
- Open source SPL enhances mangodb computing
- 【高等数学】【1】函数、极限、连续
- CarSim simulation quick start (XIV) - CarSim Simulink joint simulation
- 程序的编译和运行
- MySQL 日期【加号/+】条件筛选问题
- Question and answer 47: geeks have an appointment - the current monitoring system construction of CSC
- Recommended books | essentials of industrial digital transformation: methods and Practice
猜你喜欢

Illustration leetcode - 3. longest substring without repeated characters (difficulty: medium)
![[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

How to obtain the subordinate / annotation information of KEGG channel
![MySQL date [plus sign / +] condition filtering problem](/img/86/aed048e27b3e0b0baa919204bc067c.png)
MySQL date [plus sign / +] condition filtering problem

增加 swap 空间

Card link

【高等数学】【1】函数、极限、连续

During the interview, I was asked how to remove the weight of MySQL, and who else wouldn't?
![[advanced mathematics] [8] differential equation](/img/83/b6b07540e3cf6d6433e57447d42ee9.png)
[advanced mathematics] [8] differential equation
![[advanced mathematics] [3] Application of differential mean value theorem and derivative](/img/a9/3b024dbbb201bee4eed6c9f6ce3001.png)
[advanced mathematics] [3] Application of differential mean value theorem and derivative
随机推荐
网络RTK无人机上机测试[通俗易懂]
Mobile web layout method
SecureCRT garbled code solution [easy to understand]
Jmeter——接口测试
[tensorrt] trtexec tool to engine
网络协议:TCP Part2
[advanced mathematics] [4] indefinite integral
The database empties the table data and makes the primary key start from 1
MySQL 日期【加号/+】条件筛选问题
[today in history] July 13: the father of database passed away; Apple buys cups code; IBM chip Alliance
process.env
【高等数学】【5】定积分及应用
Introduction and construction of consul Registration Center
[today in history] June 30: von Neumann published the first draft; The semiconductor war in the late 1990s; CBS acquires CNET
Question and answer 47: geeks have an appointment - the current monitoring system construction of CSC
Kubernetes进阶部分学习笔记
Today's sleep quality record 75 points
Has baozi ever played in the multi merchant system?
Timing analysis and constraints based on xlinx (1) -- what is timing analysis? What are temporal constraints? What is temporal convergence?
【高等数学】【8】微分方程