这是悦乐书的第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(2个1,2是素数)

7 --> 111(3个1,3是素数)

9 --> 1001(2个1,2是素数)

10 --> 1010(2个1,2是素数)

输入:L = 10,R = 15

输出:5

说明:

10 --> 1010(2个1,2是素数)

11 --> 1011(3个1,3是素数)

12 --> 1100(2个1,2是素数)

13 --> 1101(3个1,3是素数)

14 --> 1110(3个1,3是素数)

15 --> 1111(4个1,4不是素数)



注意

  • L,R是[1,10 ^ 6]范围内的整数,并且L小于等于R.

  • R减L的差最多为10000。

本次解题使用的开发工具是eclipse,jdk使用的版本是1.8,环境是win7 64位系统,使用Java语言编写和测试。

02 第一种解法

第一步,需要先计算出整数的二进制数中1的个数,借助包装类Integer的bitCount方法来完成。

第二步,判断第一步获取的1的个数是否是一个素数,通过一个辅助方法来实现,如果是素数,计数count加1。

public int countPrimeSetBits(int L, int R) {
int count = 0;
for (int i=L; i<=R; i++) {
int cnt = Integer.bitCount(i);
if (isPrime(cnt)) {
count++;
}
}
return count;
} /**
* 素数:只能被1和自身整除。
* @param n
* @return
*/
public boolean isPrime(int n) {
if (n <= 3) {
return n > 1;
}
for (int i=2; i<=Math.sqrt(n); i++) {
if (n%i == 0) {
return false;
}
}
return true;
}

03 第二种解法

题目给定了L和R的范围,最大为1000000,而1000000的二进制数为11110100001001000000,其长度为20,也就是在题目给定的范围内,任意整数的二进制数中1的个数不会超过20个,而1到20内的素数只包含{2,3,5,7,11,13,17,19}这8个,我们只用判断是否是这8个数中的一个即可。

public int countPrimeSetBits2(int L, int R) {
int count = 0;
for (int i=L; i<=R; i++) {
int cnt = Integer.bitCount(i);
if (cnt == 2 || cnt == 3 || cnt == 5 || cnt == 7 ||
cnt == 11 || cnt == 13 || cnt == 17 || cnt == 19) {
count++;
}
}
return count;
}

04 第三种解法

和第二种解法的思路类似,将20个数变成布尔类型的数组,计算出二进制数中1的个数当做数组的下标去匹配。

public int countPrimeSetBits3(int L, int R) {
int count = 0;
boolean[] arr = { false, false, true, true, false,
true, false, true, false, false, false, true,
false, true,false, false, false, true, false, true };
for (int i = L; i <= R; i++) {
int cnt = Integer.bitCount(i);
if (arr[cnt]) {
count++;
}
}
return count;
}

05 小结

算法专题目前已日更超过五个月,算法题文章180+篇,公众号对话框回复【数据结构与算法】、【算法】、【数据结构】中的任一关键词,获取系列文章合集。

以上就是全部内容,如果大家有什么好的解法思路、建议或者其他问题,可以下方留言交流,点赞、留言、转发就是对我最大的回报和支持!

LeetCode算法题-Prime Number of Set Bits in Binary Representation(Java实现)的更多相关文章

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

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历数字+质数判断 日期 题目地址:https:// ...

  2. 【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: i ...

  3. [LeetCode] 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 ...

  4. LeetCode算法题-Largest Number At Least Twice of Others(Java实现)

    这是悦乐书的第308次更新,第328篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第177题(顺位题号是747).在给定的整数数组中,总有一个最大的元素.查找数组中的最大 ...

  5. 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 ...

  6. [LeetCode&Python] Problem 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 ...

  7. 762. Prime Number of Set Bits in Binary Representation二进制中有质数个1的数量

    [抄题]: Given two integers L and R, find the count of numbers in the range [L, R] (inclusive) having a ...

  8. [Swift]LeetCode762. 二进制表示中质数个计算置位 | 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 ...

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

    思路:动态规划.注意1024*1024>10^6,所以质素范围是(0,23). class Solution { public int countPrimeSetBits(int L, int ...

随机推荐

  1. macbook 添加快捷启动服务

    来至 Mac OS X: Launch Terminal from keyboard shortcut os x 上很多功能都可以通过Apple自家的Automator.app创建,且使用此方法可以为 ...

  2. Ubuntu系统下解决“YourUserName不在sudoers文件中。此事将被报告”的问题

    本文由荒原之梦原创,原文链接:http://zhaokaifeng.com/?p=624 问题描述: 之前在使用Fedora系统时遇到过在使用 sudo 时提示"YourUserName不在 ...

  3. 一个bug分析 ----------换个角度,有另外一个天地

    有个接口是按修改时间(updated_time)排序的 优化后,有人反馈接口的返回值有问题 查了一下,反馈的数据是推荐过的(推荐操作是会更新updated_time的). 然后就认为是有人进行了推荐操 ...

  4. bootstrap模态框内容替换时会重新触发模态框?<a>标签到底有哪些特殊的特性呢?

    segmentfault提问 这个问题我将bootstrap导航栏的<a>去除就解决了,那么问题来了,<a>标签到底有哪些特殊的特性呢? 主要属性href 链接href 这是一 ...

  5. 常见js特效的思路

    1.焦点轮播路 1.布局:父容器用overflow:hidden隐藏多余的图片 2:通过ID获取到重要的元素(父容器.图片列表.左右切换按钮等) 给左右按钮加上点击事件,通过JS更新图片的位置,判断边 ...

  6. Python 字典(Dictionary) has_key()方法

    描述 Python 字典(Dictionary) has_key() 函数用于判断键是否存在于字典中,如果键在字典dict里返回true,否则返回false. 语法 has_key()方法语法:dic ...

  7. Python random() 函数

    描述 random() 方法返回随机生成的一个实数,它在[0,1)范围内. 语法 以下是 random() 方法的语法: import random random.random() 注意:random ...

  8. (二)Maven的安装与环境配置

    主要内容 在Windows上安装Maven 安装目录分析 在Windows上安装Maven 本机环境:Windows10,JDK9.0.4. 想要安装Maven,需要下载Maven的zip文件,并将其 ...

  9. 用Java为Hyperledger Fabric(超级账本)开发区块链智能合约链代码之部署与运行示例代码

    部署并运行 Java 链代码示例 您已经定义并启动了本地区块链网络,而且已构建 Java shim 客户端 JAR 并安装到本地 Maven 存储库中,现在已准备好在之前下载的 Hyperledger ...

  10. sonyflake.go

        time := id >> (BitLenSequence + BitLenMachineID)     sequence := id & maskSequence > ...