Number of 1 Bits
看到题目的第一反应,是去找Python标准库中的转换函数,
果然有一个bin的函数,能将数值转换为字符
实际上,可以考虑用等长数值1以及移位操作来获得最后结果。
这样会省去创建字符串的时间,
在C下会快很多
class Solution(object):
def hammingWeight(self, n):
"""
:type n: int
:rtype: int
"""
return sum(map(int, list(bin(n))[2:]))