[Leetcode] 762. Prime Number of Set Bits in Binary Representation 解题报告

LeetCode --- 762. Prime Number of Set Bits in Binary Representation 解题报告 Given two integersLandR, find the count of numbers in the range[L, R](inclusive) having a prime number of set bits in their binary representation. (Recall that the number of set bits an integer has is the number of1s present when written in binary. ... 阅读详情

题目:

Given two integers L and R, find the count of numbers in the range [L, R] (inclusive) having a prime number of set bits in their binary representation.

(Recall that the number of set bits an integer has is the number of 1s present when written in binary. For example, 21 written in binary is 10101 which has 3 set bits. Also, 1 is not a prime.)

Example 1:

Input: L = 6, R = 10
Output: 4
Explanation:
6 -> 110 (2 set bits, 2 is prime)
7 -> 111 (3 set bits, 3 is prime)
9 -> 1001 (2 set bits , 2 is prime)
10->1010 (2 set bits , 2 is prime)

Example 2:

Input: L = 10, R = 15
Output: 5
Explanation:
10 -> 1010 (2 set bits, 2 is prime)
11 -> 1011 (3 set bits, 3 is prime)
12 -> 1100 (2 set bits, 2 is prime)
13 -> 1101 (3 set bits, 3 is prime)
14 -> 1110 (3 set bits, 3 is prime)
15 -> 1111 (4 set bits, 4 is not prime)

Note:

  1. L, R will be integers L <= R in the range [1, 10^6].
  2. R - L will be at most 10000.

思路:

暴力做法:我们从L到R,依次计算每个数中有多少个bits,如果发现它有素数个bits,就更新结果。由于int是32位的,所以我们只需要检测小于32的素数即可,为了加速处理,我们将其加入到一个哈希表中,每次只需要查找bits是否已经存在于哈希表中即可。

代码:

class Solution {
public:
    int countPrimeSetBits(int L, int R) {
        unordered_set<int> primes = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29};
        int count = 0, bits = 0;
        for (int i = L; i <= R; ++i) {
            bits = 0;
            for (int n = i; n > 0; n >>= 1) {     // calcualte the count of bits in i
                bits += n & 1;
            }
            count += primes.count(bits);
        }
        return count;
    }
};


CST微波工作室端口设置保姆级教程:从波导端口到离散端口,手把手教你搞定S参数仿真 本文提供CST微波工作室端口设置的详细教程,涵盖波导端口和离散端口的选择与配置,助力工程师精准完成S参数仿真。通过微带线滤波器案例,详解端口尺寸、阻抗设置及常见问题排查,提升仿真效率与准确性。 阅读详情

相关推荐

OSError We couldn‘t connect to https://huggingface.co to load file, couldn‘t find it chatglm3-6b解决方案

本文主要介绍了OSError: We couldn’t connect to https://huggingface.co to load this file, couldn’t find it in the cached files and it looks like THUDM/chatglm3-6b is not the path to a directory containing a file named config.json.解决方案,希望能对学习python的同学们有所帮助。

weixin_43178406的博客 5万+

762. 二进制表示中质数个计算置位 C++(位运算,bitset)

给定两个整数 L 和 R ,找到闭区间 [L, R] 范围内,计算置位位数为质数的整数个数。 (注意,计算置位代表二进制表示中1的个数。例如 21 的二进制表示 10101 有 3 个计算置位。还有,1 不是质数。) 示例 1: 输入: L = 6, R = 10 输出: 4 解释: 6 -> 110 (2 个计算置位,2 是质数) 7 -> 111 (3 个计算置位,3 是质数) 9 -> 1001 (2 个计算置位,2 是质数) 10-> 1010 (2 个计算置位,2 是质数)

ganlanA的博客 286

第244篇 视觉SLAM后端之BA优化——Bundle Adjustment的原理

本文介绍了SLAM后端优化中的Bundle Adjustment(BA)技术。BA通过同时优化相机位姿和3D路标点位置,最小化重投影误差,解决前端估计的累积误差问题。文章详细讲解了BA的数学原理,包括目标函数构建、Gauss-Newton法和Levenberg-Marquardt法求解过程,以及利用稀疏性和Schur补加速计算的关键技术。对比了全量BA和增量BA的优缺点,并介绍了Ceres、g2o等工程实现工具。BA作为SLAM后端的核心算法,其理解深度直接影响系统性能优化能力。

郑伟强dev的专栏 354

leetcode762

不看题解真不知道还有__builtin_popcount(i)这个内建函数,他的作用我理解的是:将参数展开成二进制,这个函数会自动统计这个二进制中的1的个数。然后按照题目,计算出这个数后,我们在判断这个数是不是质数即可,是的话自增。 class Solution { public: bool isprim(int x)//筛选质数 { //质数:除了1和他本身 if(x<2) { return false;

weixin_55566416的博客 807

效果炸裂_安装midscene_copw基于midscene操作电脑_基于视觉的软件测试执行_MIDSCENE的安装和使用---AI大模型应用探索0012

摘要:本文介绍了如何通过安装MidSceneJS来增强Copaw的屏幕操作能力。首先需要安装Node.js和npm,并配置淘宝镜像源加速安装。然后全局安装MidScene CLI工具(npm install @midscene/cli -g)。接着配置视觉大模型服务,通过设置环境变量指定模型API密钥、名称、基础URL和家族类型(如qwen3.5-27b),配置文件需存放在C:\Users\Administrator.copaw目录。安装成功后,MidScene已能成功识别并操作屏幕,使用效果良好。文中还提

添柴程序猿的专栏 644

【Agent实战】Agent Skills 深度解析:从原理到落地的完整指南

本文深入解析了Agent Skills的概念及其在AI协作中的实际应用。Skill是一种可被AI按需发现和加载的任务执行说明书,它将团队经验、规则和步骤固化为结构化文档,如SKILL.md。文章阐明了Skill与Prompt、MCP、Function Calling的关系,强调Skill专注于"怎么做"而非底层能力。重点介绍了SKILL.md的编写规范,包括元数据命名规则、描述技巧和正文内容优化,旨在帮助开发者高效构建可复用的知识资产,提升Agent的任务执行稳定性与专业性。通过清晰的示例和对比,为AI架构

少说,多做 121

[Leetcode每日一题]762. 二进制表示中质数个计算置位

题目: 给你两个整数 left 和 right ,在闭区间 [left, right] 范围内,统计并返回 计算置位位数为质数 的整数个数。 计算置位位数 就是二进制表示中 1 的个数。 例如, 21 的二进制表示 10101 有 3 个计算置位。 示例 1: 输入:left = 6, right = 10 输出:4 解释: 6 -> 110 (2 个计算置位,2 是质数) 7 -> 111 (3 个计算置位,3 是质数) 9 -> 1001 (2 个计算置位,2 是质数) 10->

Puppet__的博客 305

python实现马丁策略(股市) (附完整源码)

python实现马丁策略(股市) (附完整源码)

希望我的博客,能帮上你解决学习中工作中所遇到的问题 606

《LeetCode刷题》762. 二进制表示中质数个计算置位(java篇)

《LeetCode刷题》762. 二进制表示中质数个计算置位(java篇)

wqndxjr的博客 1322

基于STM32设计的智能家居系统(采用ESP8266+OneNet云平台)

一、环境介绍 单片机采用:STM32F103C8T6 上网方式:采用ESP8266,也可以使用其他设备代替,只要支持TCP协议即可。比如:GSM模块、有线网卡等。 云平台: 采用中国移动OneNet. 也可以采用腾讯、阿里云、华为云、百度天工物接入、机智云等等。 前面文章有讲解。 协议: 采用MQTT协议 开发软件:keil5 完整项目源码下载:https://download.csdn.net/download/xiaolong1126626497/19766925 二、包含的硬件与实...

1万+

使用libVLC进行视频截图的C/C++实现

通过这些代码,你可以在C/C++应用程序中实现视频截图功能,并将视频帧保存为图像文件。请注意,本文只提供了基本的示例代码,你可以根据自己的需求进行扩展和修改。libVLC是一个强大的开源多媒体框架,可以用于在C/C++应用程序中处理和播放音频和视频文件。我们将展示如何初始化libVLC,打开视频文件,捕获视频帧并保存为图像文件。在开始编写代码之前,你需要包含libVLC的头文件,并链接相应的库文件。在链接阶段,你需要添加libVLC库的链接选项。在使用libVLC之前,你需要初始化libVLC的实例。

学习使你进步。 471

【LeetCode】762. Prime Number of Set Bits in Binary Representation 解题报告(Python)

【LeetCode】762. Prime Number of Set Bits in Binary Representation 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/prime-number-of-set-bits-in-binary-representation/description/ 题目描述:

负雪明烛 1895

【AutoCAD中实现双击文本编辑功能】

例如:以下是官方博客例子浅析先加入相关的头文件和库文件。在AutoCAD中有一个很重要的功能,就是双击图形尺寸或文本后会出现一个文本编辑框,用户可在该编辑框中修改文字。本文介绍了如何使用AcEdInplaceTextEditor来实现该功能。实现cad的消息监听。创建一个消息监听的类并在cad中注册来实现消息监听。主要是监听键盘输入。提示:以下是本篇文章正文内容,下面案例可供参考提示:这里对文章进行总结:例如:以上就是今天要讲的内容,本文仅仅简单介绍了的函数和方法。

我的博客 1149

LeetCode //C - 762. Prime Number of Set Bits in Binary Representation

This problem requires counting numbers in a given range [left, right] that have a prime number of set bits (1's) in their binary representation. The solution involves three key steps: checking if a number is prime, counting the set bits of a number, and it

Made in Code 1237

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

【LeetCode】762. Prime Number of Set Bits in Binary Representation

Python版本:python3.6.2 762. Prime Number of Set Bits in Binary Representation Given two integers L and R, find the count of numbers in the range [L, R] (inclusive) having a prime number of set b

sunflower_kris的博客 392

怀旧版电脑GBC模拟器

再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。

【Leetcode_easy】762. Prime Number of Set Bits in Binary Representation

problem 762.Prime Number of Set Bits in Binary Representation solution1: class Solution { public: int countPrimeSetBits(int L, int R) { int res = 0; int bits = 0; ...

weixin_30342827的博客 134

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

【Leetcode】762. Prime Number of Set Bits in Binary Representation

题目地址: https://leetcode.com/problems/prime-number-of-set-bits-in-binary-representation/ 给定一个正整数区间[L,R][L,R][L,R],问其有多少个整数的二进制表示111的个数是素数。 代码如下: import java.util.Arrays; public class Solution { public int countPrimeSetBits(int L, int R) { boolea

数学、算法爱好者的博客 183

[Leetcode] 762. Prime Number of Set Bits in Binary Representation

Description: Given two integers L and R, find the count of numbers in the range [L, R] (inclusive) having a prime number of set bits in their binary representation. (Recall that the number of set bi...

ApRiLLLLLLLLLLLL的博客 265

LeetCode算法题-Prime Number of Set Bits in Binary Representation(Java实现)

这是悦乐书的第311次更新,第332篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第180题(顺位题号是762)。给定两个正整数L和R,在[L,R]范围内,计算每个整数的二进制数中1的个数,判断1的个数是否是一个素数。例如,21的二进制数是10101,其中1的个数有3个,3是一个素数。例如: 输入:L = 6,R = 10 输出:4 说明: 6 --> 110...

小川的博客 603

LeetCode C++ 762. Prime Number of Set Bits in Binary Representation【位操作】简单

Given two integers L and R, find the count of numbers in the range [L, R] (inclusive) having a prime number of set bits in their binary representation. (Recall that the number of set bits an integer has is the number of 1s present when written in binary..

memcpy0的博客 409

【LeetCode】762. Prime Number of Set Bits in Binary Representation(C++)

地址:https://leetcode.com/problems/reach-a-number 题目: Given two integers L and R, find the count of numbers in the range [L, R] (inclusive) having a prime number of set bits in their binary representati...

Ethan95的博客 234

python leetcode 762. Prime Number of Set Bits in Binary Representation

Given two integers L and R, find the count of numbers in the range [L, R] (inclusive) having a prime number of set bits in their binary representation.(Recall that the number of set bits an integer ha...

DreamerLHQ的博客 419

#Leetcode# 762. Prime Number of Set Bits in Binary Representation

https://leetcode.com/problems/prime-number-of-set-bits-in-binary-representation/ Given two integersLandR, find the count of numbers in the range[L, R](inclusive) having a prime number of set b...

weixin_30530939的博客 85

LeetCode - easy-762. Prime Number of Set Bits in Binary Representation

竞赛问题 Given two integers L and R, find the count of numbers in the range [L, R] (inclusive) having a prime number of set bits in their binary representation. (Recall that the number of set bits an in

再见二丁目 388

LeetCode 762. 762. Prime Number of Set Bits in Binary Representation

题目链接:二进制表示中质数个计算置位 - 力扣 (LeetCode) Given two integers L and R, find the count of numbers in the range [L, R] (inclusive) having a prime number of set bits in their binary representation. (Recall that ...

zhaohaibo的博客 338

leetcode 762. Prime Number of Set Bits in Binary Representation

基本的代码组合 class Solution { public int countPrimeSetBits(int L, int R) { int count = 0; for(int i=L;i<=R;i++){ int count1 = 0; int tmp = i; whil...

xiaobailaji的博客 130
上一篇: [Leetcode] 761. Special Binary String 解题报告
下一篇: [Leetcode] 763. Partition Labels 解题报告
魔豆Magicbean
博客等级 码龄19年 146粉丝 742原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值