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 ...
 
随机推荐
- 基于Jmeter+maven+Jenkins构建性能自动化测试平台
			
一.目的: 为能够将相关系统性能测试做为常规化测试任务执行,且可自动无人值守定时执行并输出性能测试结果报告及统计数据,因此基于Jmeter+maven+Jenkins构建了一套性能自动化测试平台 ...
 - 剑指offer(javascript实现)
			
1.二维数组中的查找 在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数. funct ...
 - 运维监控利器Nagios之:nagios配置详解
			
http://ixdba.blog.51cto.com/2895551/752870 一.nagios配置过程详解 1.nagios默认配置文件介绍 nagios安装完毕后,默认的配置文件在/usr ...
 - centos7系统日志时间与系统时间相差8小时
			
场景:当我们修改完系统时间的时区后,我们去查看我们的系统日志的时间发现时区还是在之前的系统时间时区. [root@vp-n ~]# ls -l /etc/localtime lrwxrwxrwx 1 ...
 - bzoj 4501 旅行
			
01分数规划+最大权闭合子图 倒拓扑序处理每个节点 $$f[x]=\frac{\sum{f[v]}}{n}+1$$ 二分答案$val$ 只需要判断是否存在$\sum{f[v]}+1-val>0$ ...
 - BZOJ_2151_种树_贪心+堆+链表
			
BZOJ_2151_种树_贪心+堆 Description A城市有一个巨大的圆形广场,为了绿化环境和净化空气,市政府决定沿圆形广场外圈种一圈树.园林部门得到指令后,初步规划出n个种树的位置,顺时针编 ...
 - BZOJ_4892_[Tjoi2017]dna_哈希
			
BZOJ_4892_[Tjoi2017]dna_哈希 Description 加里敦大学的生物研究所,发现了决定人喜不喜欢吃藕的基因序列S,有这个序列的碱基序列就会表现出喜欢吃藕的 性状,但是研究人员 ...
 - Micropython  TPYBoard ADC的使用方法
			
基本用法 import pybadc = pyb.ADC(Pin('Y11')) # create an analog object from a pinadc = pyb.ADC(pyb.Pin.b ...
 - gitlab-ci-runner安装
			
前言 什么是CI/CD? CI (Continuous Integration) 持续集成, CD (Continuous Delivery) 持续部署 个人理解 本地开发代码, 提交远程仓库 仓库接 ...
 - springboot mybatis 多数据源配置
			
首先导入mybatis等包,这里就不多说. 下面是配置多数据源和mybatis,每个数据源对应一套mybatis模板 数据源1: package com.aaaaaaa.config.datasour ...