Leetcode 762. Prime Number of Set Bits in Binary Representation

Posted Deribs4

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Leetcode 762. Prime Number of Set Bits in Binary Representation相关的知识,希望对你有一定的参考价值。

 

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

 1 class Solution {
 2     public int countPrimeSetBits(int L, int R) {
 3         Set<Integer> prime = new HashSet<Integer>(Arrays.asList(2,3,5,7,11,13,17,19,23));
 4         int[] count = new int[1+R];
 5         int res = 0;
 6         for(int i = 1; i <=R; i++) count[i] = count[i>>1] + (i&1);
 7         for(int i = L; i <=R; i++) {
 8             if(prime.contains(count[i])) res++;
 9         }
10         return res;
11     }
12 }

 

以上是关于Leetcode 762. Prime Number of Set Bits in Binary Representation的主要内容,如果未能解决你的问题,请参考以下文章