这是悦乐书的第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. jQuery-01:on live bind delegate

    摘自:https://www.cnblogs.com/moonreplace/archive/2012/10/09/2717136.html moonreplace这位大牛的 当我们试图绑定一些事件到 ...

  2. 深入出不来nodejs源码-编译启动(1)

    整整弄了两天,踩了无数的坑,各种奇怪的error,最后终于编译成功了. 网上的教程基本上都过时了,或者是版本不对,都会报一些奇怪的错误,这里总结一下目前可行的流程. node版本:v10.1.0. 首 ...

  3. maven入门 (二)_私服安装与上传下载

    本篇文章主要介绍maven的私服安装和 jar包的上传与下载.毕竟大家还是在公司需要上传jar包到自己公司私服的. 1.安装私服 下载链接: https://pan.baidu.com/s/17dbQ ...

  4. Oracle .NET Core Beta驱动已出,自己动手写EF Core Oracle

    使用.net core也有一段时间了,一直都没有Oracle官方的正式版驱动程序,更别说EF版本了.之前基于Oracle官方的.net core预览版本写了个Dapper的数据库操作实现,但是总感觉不 ...

  5. Java VisualVM无法检测到本地java程序 的 解决办法

    win10系统下启动jvisualvm应用,报"VisualVM无法检测到本地java程序"的错误!在网上查了一些方法, 大概原因有2种: 1.操作系统的临时文件目录所在的磁盘格式 ...

  6. Matlab与C混编的介绍

    原本写给一个朋友的,帮助她入门matlab与C混编的 >#####环境: * Matlab:MATLAB R2013a * C编译器VC++2012 === #####配置环境: 在**Matl ...

  7. text-shadow 详解及示例

    text-shadow  [tɛkst] - [ˈʃædoʊ]   定义: text-shadow: none | <shadow> [,<shadow>]* <shad ...

  8. Ubuntu基础教程——安装谷歌Chrome浏览器

    对于刚刚开始使用Ubuntu并想安装谷歌Chrome浏览器的新用户来说,本文所介绍的方法是最快捷的.在Ubuntu上安装谷歌Chrome的方法有很多.一些用户喜欢直接在 谷歌Chrome下载页面 获得 ...

  9. TestNG深入理解

    以下内容引自: http://blog.csdn.net/wanglha/article/details/42004695 TestNG深入理解 转载 2014年12月18日 13:56:11 参考文 ...

  10. 浅谈cookie,sessionStorage和localStorage

    cookie:cookie在浏览器和服务器间来回传递 cookie数据不能超过4k 同时每次http请求都会携带cookie,所以cookie只适合保存很小的数据,比如会话标识 cookie只在设置的 ...