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,素数判断部分是经典的方法。
相关文章推荐
- LeetCode之Prime Number of Set Bits in Binary Representation(Kotlin)
- Prime Number of Set Bits in Binary Representation
- 762. Prime Number of Set Bits in Binary Representation
- Prime Number of Set Bits in Binary Representation
- 762. Prime Number of Set Bits in Binary Representation
- 762. Prime Number of Set Bits in Binary Representation
- Counting-the-number-of-set-bits-in-an-integer
- [CareerCup] 5.2 Binary Representation of Real Number 实数的二进制表示
- Count the number of bits that are on in an unsigned integer(计算一个无符整数中1Bit的个数)-- (1)
- How to set number of threads in OpenMP?
- 1.求整数最大的连续0的个数 BinaryGap Find longest sequence of zeros in binary representation of an integer.
- Number of Bits in a Decimal Integer
- Binary representation of a given number
- win10 ise 生成bit文件报错FATAL_ERROR:Bitstream:stanbsbitfile.c:3408:1.57 - Incorrect number of bits in
- Next higher number with same number of set bits
- LC-Average of Levels in Binary Tree
- Find the number of subsets such that the sum of numbers in the subset is a prime number
- NumberOfOneInBinary
- 数据中聚类个数的确定(Determining the number of clusters in a data set)
- count the number of "1" in the binary figure