剑指Offer11:二进制中1的个数

思路:

将n每一位与1相与,若为0则0,若为1则1,并存下来,最后求和。

# -*- coding:utf-8 -*-
class Solution:
    def NumberOf1(self, n):
        # write code here
        return sum([(n>>i & 1) for i in range(0,32)])

猜你喜欢

转载自blog.csdn.net/weixin_43160613/article/details/84578747