191. Number of 1 Bits

Question 191

https://leetcode.com/problems/number-of-1-bits/description/

Write a function that takes an unsigned integer and returns the number of '1' bits it has (also known as the Hamming weight).

Example 1:

Input: 11
Output: 3
Explanation: Integer 11 has binary representation 00000000000000000000000000001011 

Answer

class Solution(object):
    def hammingWeight(self, n):
        AnswerINT = 0
        while(n):
            if(n%2):
                AnswerINT += 1
            n = n // 2
        return AnswerINT

Last updated