Leetcode 762. Prime Number of Set Bits in Binary Representation

Leetcode 762. Prime Number of Set Bits in Binary Representation

思路:动态规划。注意1024*1024>10^6,所以质素范围是(0,23)。

 class Solution {
public int countPrimeSetBits(int L, int R) {
Set<Integer> prime = new HashSet<Integer>(Arrays.asList(2,3,5,7,11,13,17,19,23));
int[] count = new int[1+R];
int res = 0;
for(int i = 1; i <=R; i++) count[i] = count[i>>1] + (i&1);
for(int i = L; i <=R; i++) {
if(prime.contains(count[i])) res++;
}
return res;
}
}
上一篇:Oracle11G登录时提示:ORA-12557: TNS:协议适配器不可加载


下一篇:【Leetcode_easy】762. Prime Number of Set Bits in Binary Representation