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.pyLeetcode 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.