码迷,mamicode.com
首页 > 其他好文 > 详细

Leetcode 762. Prime Number of Set Bits in Binary Representation

时间:2018-01-15 00:30:50      阅读:118      评论:0      收藏:0      [点我收藏+]

标签:规划   src   http   prime   col   for   contains   prim   info   

技术分享图片

 

思路:动态规划。注意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

标签:规划   src   http   prime   col   for   contains   prim   info   

原文地址:https://www.cnblogs.com/Deribs4/p/8284761.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!