当前位置:网站首页>leetcode:6127. 优质数对的数目【位运算找规律 + 两数之和大于等于k + 二分】
leetcode:6127. 优质数对的数目【位运算找规律 + 两数之和大于等于k + 二分】
2022-07-25 15:44:00 【白速龙王的回眸】

分析
首先位运算必然找规律
找到规律为a or b + a and b的1的个数 = a的1个数 + b的1个数
问题转化为有多少种组合使得num1和num2的1的个数大于等于k
使用排序加二分即可轻松解决
ac code
class Solution:
def countExcellentPairs(self, nums: List[int], k: int) -> int:
# 位运算找规律
# or + and的1之和 = 原两个num1之和
# 1 1: 2 == 2
# 1 0: 1 == 1
# 0 0: 0 == 0
cnts = []
nums = set(nums)
for num in nums:
cnts.append(bin(num)[2:].count('1'))
cnts.sort()
ans = 0
#print(cnts)
for i, v in enumerate(cnts):
least = k - v
idx = bisect_left(cnts, least)
rest = len(cnts) - idx
#print(i, v, least, idx, rest)
ans += rest
return ans
总结
关键在于问题的转化
边栏推荐
- JWT diagram
- Ml image depth learning and convolution neural network
- Geogle colab notes 1-- run the.Py file on the cloud hard disk of Geogle
- 谷歌博客:采用多重游戏决策Transformer训练通用智能体
- "Digital security" alert NFT's seven Scams
- [server data recovery] data recovery cases of raid information loss caused by unexpected power failure of HP EVA server storage
- MySQL教程65-MySQL操作表中数据
- Leetcode - 359 log rate limiter (Design)
- How to solve cross domain problems
- 2022-07-25日报:微软提出CodeT:代码生成新SOTA,20个点的性能提升
猜你喜欢

Leetcode - 707 design linked list (Design)

Leetcode - 362 knock counter (Design)

通用测试用例写作规范

Solve the vender-base.66c6fc1c0b393478adf7.js:6 typeerror: cannot read property 'validate' of undefined problem

不愧是阿里内部“千亿级并发系统架构设计笔记”面面俱到,太全了

推荐收藏,这或许是最全的类别型特征的编码方法总结

Boomi荣获“多元化最佳首席执行官奖”和“职业成长最佳公司奖”,在大型公司类别中跻身50强

How Google cloud disk is associated with Google colab

SVD singular value decomposition derivation and application and signal recovery

LeetCode - 677 键值映射(设计)*
随机推荐
< stack simulation recursion >
I want to ask whether the variable configuration function can only be used in SQL mode
物理防火墙是什么?有什么作用?
记得那两句话
R语言ggplot2可视化线图(line)、自定义配置标题文本相关内容颜色和图例(legend)颜色相匹配(和分组线图的颜色相匹配、match colors of groups)
「数字安全」警惕 NFT的七大骗局
PageHelper.startPage没有生效问题
Endnote cannot edit range resolution
共享锁(Shared Lock)
mysql 表读锁
百奥赛图与LiberoThera共同开发全人GPCR抗体药物取得里程碑式进展
Componentization and modularization
# JWT 图解
Boomi荣获“多元化最佳首席执行官奖”和“职业成长最佳公司奖”,在大型公司类别中跻身50强
十字链表的存储结构
Pytoch learning notes -- seresnet50 construction
Why is preparestatement better and safer?
R语言使用gt包和gtExtras包漂亮地显示表格数据:gt_bar_plot函数和gt_plt_bar_pct函数可视化百分比条形图、原始数据的百分比条形、缩放后的数据的百分比条形、指定数据对齐宽度
Alibaba's internal "100 billion level concurrent system architecture design notes" are all inclusive, too comprehensive
LeetCode - 707 设计链表 (设计)