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-01:基础命令小结
------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 记录一下我的Oracle学习之路,详细的安装教程忙完这俩天会认真总结一版 本次记录这次学习cmd基础命令 一, ...
- spring-boot-oracle spring-batch
Install/Configure Oracle express Oracle xe installer for linux (I don't care if you're running linux ...
- is not eligible for getting processed by all BeanPostProcessors
BeanPostProcessor是控制Bean初始化开始和初始化结束的接口.换句话说实现BeanPostProcessor的bean会在其他bean初始化之前完成,BeanPostProcessor ...
- nrpe参数传递
如果需要nrpe进行参数传递需要主要一下几个方面: 1.编译nrpe的时候需要增加参数--enable-command-args 2.修改nrpe.cfg文件中参数dont_blame_nrpe=1 ...
- SourceTree安装跳过注册
问题描述 SourceTree是一个可视化管理版本文件的工具,但是注册的时候需要打开外国的网站. 因此要安装时需要绕过这一步才能使用. 解决方案 1. 在下面路径下创建一个accounts.json文 ...
- java8中stream的map和flatmap的理解
转自https://blog.csdn.net/wynjauu/article/details/78741093 假如我们有这样一个需求给定单词列表["Hello","W ...
- 在docker上运行.net core程序
一.安装docker及镜像 1.在centos上安装docker,命令如下: # yum install docker 2.让docker随机启动: # service docker start# c ...
- 在使用MFC对话框时遇到的用户交互
有时我们需要点击对话框上的一个按钮的时候,对话框隐藏起来,然后执行完某个操作后,对话框又弹出来, 这时我们可以使用BeginEditorCommand()和CompleteEditorCommand( ...
- bzoj 3551 kruskal重构树dfs序上的主席树
强制在线 kruskal重构树,每两点间的最大边权即为其lca的点权. 倍增找,dfs序对应区间搞主席树 #include<cstdio> #include<cstring> ...
- BZOJ_1954_Pku3764 The xor-longest Path_Trie树
Description 给定一棵n个点的带权树,求树上最长的异或和路径 把根到点路径上点权异或和求出来,然后变成了Trie树裸题. 代码: #include <cstdio> #inc ...