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(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实现)的更多相关文章
- 【LeetCode】762. Prime Number of Set Bits in Binary Representation 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历数字+质数判断 日期 题目地址:https:// ...
- 【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 ...
- [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 ...
- LeetCode算法题-Largest Number At Least Twice of Others(Java实现)
这是悦乐书的第308次更新,第328篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第177题(顺位题号是747).在给定的整数数组中,总有一个最大的元素.查找数组中的最大 ...
- 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 ...
- [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 ...
- 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 ...
- [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 ...
- Leetcode 762. Prime Number of Set Bits in Binary Representation
思路:动态规划.注意1024*1024>10^6,所以质素范围是(0,23). class Solution { public int countPrimeSetBits(int L, int ...
随机推荐
- Oracle-02:SQL语言的分类或者说SQL语言的组成
------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 小结一版: 01.DDL(Data Definition Language)数据定义语言. 用来创建数据库中 ...
- HTML(总结)
HTML 浏览器内核有哪些 Trident:IE Gecko:Firefox Webkit:Chrome Safari Presto:Opera(投奔Webkit) html5的一些新特性 1. 拖拽 ...
- 使用Spring MVC测试Spring Security Oauth2 API
不是因为看到希望了才去坚持,而坚持了才知道没有希望. 前言 在Spring Security源码分析十一:Spring Security OAuth2整合JWT和Spring Boot 2.0 整合 ...
- FPGA学习笔记(五)—— 组合逻辑电路功能的描述
设计方法: 分析真值表规律 两种描述方式: 方式1:用assign描述,用阻塞赋值= 方式2:用always@(*)描述,用非阻塞赋值<= 选择功能的三种描述方式: ...
- bzoj 2820 莫比乌斯反演
搞了一整个晚自习,只是看懂了dalao们的博客,目前感觉没有思路-.还是要多切题 next day: 刚才又推了一遍,发现顺过来了,hahaha #include<cstdio> #inc ...
- hystrix 结果缓存机制(5)
hystrix支持将一个请求结果缓存起来,下一个具有相同key的请求将直接从缓存中取出结果,减少请求开销.要使用hystrix cache功能 第一个要求是重写getCacheKey(),用来构造ca ...
- Random在高并发下的缺陷以及JUC对其的优化
Random可以说是每个开发都知道,而且都用的很6的类,如果你说,你没有用过Random,也不知道Random是什么鬼,那么你也不会来到这个技术类型的社区,也看不到我的博客了.但并不是每个人都知道Ra ...
- C#串口通讯概念以及简单实现
最近在研究串口通讯,其中有几个比较重要的概念,RS-232这种适配于上位机和PC端进行连接,RS-232只限于PC串口和设备间点对点的通信.它很简单的就可以进行连接,由于串口通讯是异步的,也就是说你可 ...
- .net core 中间件管道底层剖析
.net core 管道(Pipeline)是什么? 由上图可以看出,.net core 管道是请求抵达服务器到响应结果返回的中间的一系列的处理过程,如果我们简化一下成下图来看的话,.net core ...
- 一个实时收集MySql变更记录的组件CanalSharp.AspNetCore
一.关于CanalSharp CanalSharp 是阿里巴巴开源项目 Canal 的 .NET 客户端.为 .NET 开发者提供一个更友好的使用 Canal 的方式.Canal 是mysql数据库b ...