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 ...
随机推荐
- 使用SimHash进行海量文本去重[转载]
阅读目录 1. SimHash与传统hash函数的区别 2. SimHash算法思想 3. SimHash流程实现 4. SimHash签名距离计算 5. SimHash存储和索引 6. SimHas ...
- 解决Android模拟器卡慢的问题
解决Android模拟器卡慢的问题 本文介绍使用Intel HAXM技术为Android模拟器加速,使模拟器运行速度媲美真机. Intel HAXM(Hardware Accelerate Execu ...
- c# Socket通信异步TCP
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.N ...
- json数据取值
var data = JSON.parse(JSON.stringify(position, null, 4));
- 10.app后端选择什么开发语言
在qq上,经常看到有创业团队的创始人一直都招不到技术人员,除了项目的因素外,很大的原因就是所需要掌握的开发语言偏门.通过阅读本文,详细了解选择开发语言的核心原则,使各位心里对开发语言的选择更加有数. ...
- redis list命令操作
1.将值追加到列表 RPUSH key value [value ...]summary: Append one or multiple values to a listsince: 1.0.0 12 ...
- BZOJ_1923_[Sdoi2010]外星千足虫_高斯消元+bitset
BZOJ_1923_[Sdoi2010]外星千足虫_高斯消元 Description Input 第一行是两个正整数 N, M. 接下来 M行,按顺序给出 Charles 这M次使用“点足机”的统计结 ...
- 【小白学C#】谈谈C#多播委托因异常而终止的解决方案
一.前言 前几天,马三在与朋友闲聊技术的时候,朋友忽然抛出一个问题,把马三难倒了,本着求知的精神,回来以后马三就查阅了相关资料并做了一些实验,终于把问题搞明白了,因此写下本篇博客记录一下.首先,问题是 ...
- Angular2+ 使用 Protractor 与 Modify Header Value (HTTP Headers) 插件 完成 Windows Authorization 验证
入职新公司第二周,接到了一个E2E测试的任务,两天的时间把所有的测试条件都写完了,结果剩下三天都卡在了Windows Authorization验证这里. 先说一下公司项目Authorize的逻辑 第 ...
- solr6.6 导入索引数据
1.什么是core core是solr的一个索引库,可以理解为一个数据库,core可以根据需要,创建多个. 2.创建core 例如,创建一个core,名字叫mycore,就可以用一下命令: E:\so ...