题目来源
题目描述
题目解析
1、10^6表示位二进制一共20位:Integer.toBinaryString((int) Math.pow(10, 6)).length()
2、0到20中是质数的有:2, 3, 5, 7, 11, 13, 17, 19
3、求二进制中1的个数:Integer.bitCount(num)
class Solution {
public int countPrimeSetBits(int L, int R) {
int[] prime = new int[]{0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1};
int cnt = 0;
for (int i = L; i <= R; i++){
cnt += prime[Integer.bitCount(i)];
}
return cnt;
}
}