LeetCode 191 Number of 1 Bits

LeetCode 191 Number of 1 Bits Problem

Download Code
class Solution(object):
    # def hammingWeight(self, n):
    #     """
    #     :type n: int
    #     :rtype: int
    #     """
    #     # using bin
    #     s_n = bin(n)[2:]
    #     return s_n.count('1')

    def hammingWeight(self, n):
        """
        :type n: int
        :rtype: int
        """
        # https://leetcode.com/articles/number-1-bits/
        count = 0
        while n:
            n &= n - 1
            count += 1
        return count
Download Number of 1 Bits.py

List of all Number of 1 Bits problems

Leetcode 191 Number of 1 Bits problem solution in python3 with explanation. This is the best place to expand your knowledge and get prepared for your next interview.

Feedback is the most important part of any website.

If you have any query, suggestion or feedback, Please feel free to contact us.