Skip to content
LC-0762 Easy LeetCode

762. Prime Number of Set Bits in Binary Representation

Read the full problem statement on LeetCode.
Difficulty: easy Acceptance: 71% Topics: Math, Bit Manipulation
View full problem on LeetCode
Reference solution (spoiler · python)
# Time:  O(log(R - L)) = O(1)
# Space: O(1)

class Solution(object):
    def countPrimeSetBits(self, L, R):
        """
        :type L: int
        :type R: int
        :rtype: int
        """
        def bitCount(n):
            result = 0
            while n:
                n &= n-1
                result += 1
            return result

        primes = {2, 3, 5, 7, 11, 13, 17, 19}
        return sum(bitCount(i) in primes
                   for i in xrange(L, R+1))

Solution from kamyu104/LeetCode-Solutions · MIT

Similar questions