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 }

 

posted @ 2018-01-14 22:56  Deribs4  阅读(214)  评论(0编辑  收藏  举报