Concise and logical solution without bit operations


  • 0
    A
    class Solution(object):
        def hammingWeight(self, n):
        	count = 0
        	for i in xrange(31, -1, -1):
        		num = n/pow(2, i)
        		if num > 0:
        			n = n % pow(2,i)
    
        			count += 1
        			
        	return count 
    
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.