您的位置:首页 > 其它

LC-Prime Number of Set Bits in Binary Representation

2018-01-17 18:48 543 查看
class Solution(object):
def countPrimeSetBits(self, L, R):
"""
:type L: int
:type R: int
:rtype: int
"""
def isPrime(n):
if n <= 1:
return False
i = 2
while i * i <= n:
if n % i == 0:
return False
i += 1
return True
count = 0
for x in range(L,R+1):
num_1 = bin(x).count('1')
if isPrime(num_1):
count += 1
return count

Sol = Solution()
print Sol.countPrimeSetBits(9,15)


0,自己写的,感觉效率还行

1,重点在于对数字的二进制转换和对1的数量的统计,用到了.count(t)函数。

2,素数判断部分是经典的方法。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: