class Solution: def NumberOf1(self, n): # write code here count=0 if n < 0: n = n & 0x7fffffff count+=1 while not n==0: count+=n&1 n=n>>1 return count
思路:先对复数进行处理,转化为正数。之后右移进行累加操作。
剑指offer-二进制中1的个数11
原文:https://www.cnblogs.com/zhaiyansheng/p/10413599.html