思路:分大于0小于0两种情况判断
1、> 0, 统计1的个数 2、< 0, 转成正数,统计0的个数,32-0的个数
class Solution:
def NumberOf1(self , n: int) -> int:
# write code here
if n == 0:
return 0
result = 0
if n > 0:
while n:
if n % 2 == 1:
result += 1
n = n // 2
return result
else:
n = -n
while n:
if n % 2 == 0:
result += 1
n = n // 2
return 32 - result